diff --git a/client/components/home/_home.jsx b/client/components/home/_home.jsx index 59389ad..02ebe38 100644 --- a/client/components/home/_home.jsx +++ b/client/components/home/_home.jsx @@ -1,3 +1,23 @@ +import { useContext } from 'react'; +import { SettingsContext } from '../../utils/settings_context'; + export const Home = () => { - return
I am the home page
; + const [, dispatch] = useContext(SettingsContext); + const logout = async () => { + const res = await fetch('/sessions', { + method: 'DELETE', + }); + if (res.status === 200) { + dispatch({ type: 'update', payload: { jwt: undefined } }); + } + }; + + return ( +
+

Welcome

+ +
+ ); }; diff --git a/client/components/router.jsx b/client/components/router.jsx index 6760bea..59ea7c3 100644 --- a/client/components/router.jsx +++ b/client/components/router.jsx @@ -7,12 +7,12 @@ import { SignUp } from './sign_up/_sign_up'; export const Router = () => { const [settings] = useContext(SettingsContext); - const { JWT } = settings; + const { jwt } = settings; return ( : } // no JWT means not logged in + element={jwt ? : } // no jwt means not logged in /> } /> } /> diff --git a/client/components/sign_in/_sign_in.jsx b/client/components/sign_in/_sign_in.jsx index 753b2b5..a6e802c 100644 --- a/client/components/sign_in/_sign_in.jsx +++ b/client/components/sign_in/_sign_in.jsx @@ -1,7 +1,9 @@ -import { useState } from 'react'; +import { useContext, useState } from 'react'; import { useNavigate } from 'react-router'; +import { SettingsContext } from '../../utils/settings_context'; export const SignIn = () => { + const [, dispatch] = useContext(SettingsContext); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const navigate = useNavigate(); @@ -10,6 +12,26 @@ export const SignIn = () => { navigate('/signup'); }; + const signIn = async () => { + const res = await fetch('/sessions', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + email, + password, + }), + }); + if (res.status === 201) { + const result = await res.json(); + dispatch({ type: 'update', payload: { jwt: result.token } }); + navigate('/'); + } else { + console.error('An issue occurred when logging in.'); + } + }; + return (
Email
@@ -25,7 +47,9 @@ export const SignIn = () => { onChange={(e) => setPassword(e.target.value)} />
- +