diff --git a/client/src/App.tsx b/client/src/App.tsx index 5e7c3a7..c0dc73e 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,11 +1,10 @@ // framework tools and custom utils -import { useCallback, useContext, useEffect, useState } from 'react'; +import { useEffect } from 'react'; import { BrowserRouter, Routes, Route } from 'react-router-dom'; -import { AuthContext, IAuthContext, useAuthContext } from './context/AuthContext'; -import { attemptLogout, checkCredientials } from './util/apiUtils'; -import { IUser } from './schemas'; +import { useAuthContext } from './context/AuthContext'; +import jwtDecode from 'jwt-decode'; -// pages, ui, styles +// pages, ui, components, styles import Subscriptions from './components/pages/Subscriptions/Subscriptions'; import Browser from './components/ui/Browser'; import Collection from './components/pages/Collection'; @@ -19,55 +18,41 @@ import CollectionBrowser from './components/pages/CollectionBrowser'; import { Navbar } from './components/ui'; import GroceryList from './components/pages/GroceryList'; import GroceryListCollection from './components/pages/GroceryListCollection'; +import { TokenType } from './util/types'; import './sass/App.scss'; function App() { - const [user, setUser] = useState(); - const parentState = { user, setUser }; - - const receiveChange = (() => {}); + const { setUser, setToken } = useAuthContext(); useEffect(() => { - const wrapper = async () => { - try { - const result: IAuthContext | undefined = await checkCredientials(); - - if (result == undefined) { - setUser({ user: undefined }); - } else { - setUser(result); - } - } catch(e) { - console.error(e); - } + if (document.cookie) { + const extractedToken: Partial = jwtDecode(document.cookie.split("=")[1]); + setToken(document.cookie.split("=")[1]); + setUser(extractedToken.user); } - - wrapper(); - }, []) + }, []); return ( - -
- - - } /> - } /> - } /> - } /> - } /> - } /> - {}} />} /> - } /> - } /> - } /> +
+ + + } /> + } /> + } /> + } /> + } /> + } /> + {}} />} /> + } /> + } /> + } /> - } /> - } /> - } /> - -
- + } /> + } /> + } /> +
+
) } diff --git a/client/src/components/pages/Login.tsx b/client/src/components/pages/Login.tsx index 4edad82..0fe25bd 100644 --- a/client/src/components/pages/Login.tsx +++ b/client/src/components/pages/Login.tsx @@ -1,10 +1,10 @@ import { useCallback, useContext, useEffect, useState } from "react"; -import { useNavigate, useParams } from "react-router-dom"; import { AuthContext, useAuthContext } from "../../context/AuthContext"; -import { attemptLogin } from "../../util/apiUtils"; +import { useNavigate, useParams } from "react-router-dom"; import { IUser, IUserAuth } from "../../schemas"; import { Button, Form, Page, Panel } from "../ui"; import { FormConfig } from "../ui/Form"; +import API from "../../util/API"; export default function Login() { const params = new URLSearchParams(window.location.search); @@ -24,9 +24,12 @@ export default function Login() { const handleLogin = async () => { if (!input.email || !input.password) return; - const { data, ok } = await attemptLogin(input); - if (ok) setUser(data); - navigate(`/${redirect ?? ''}`); + const result = await new API.Auth().login(input); + console.log(result); + + // const { data, ok } = await attemptLogin(input); + // if (ok) setUser(data); + // navigate(`/${redirect ?? ''}`); } // check for logged in user and mount form diff --git a/client/src/components/pages/Register/collection.tsx b/client/src/components/pages/Register/collection.tsx index ee97dfc..5a41012 100644 --- a/client/src/components/pages/Register/collection.tsx +++ b/client/src/components/pages/Register/collection.tsx @@ -6,7 +6,7 @@ import { attemptLogin, createNewCollection } from "../../../util/apiUtils"; import { Button, Divider, Page, Panel } from "../../ui"; import TextField from "../../ui/TextField"; -const InitialCollection: RegisterVariantType = ({ transitionDisplay, receiveChange, input }) => { +const InitialCollection: RegisterVariantType = ({ transitionDisplay, input }) => { const [collectionName, setCollectionName] = useState(); const [view, setView] = useState(

Loading...

); const [user, setUser] = useState(); @@ -45,8 +45,7 @@ const InitialCollection: RegisterVariantType = ({ transitionDisplay, receiveChan } useEffect(() => { - if (user && receiveChange) { - receiveChange(user); + if (user) { setView(

Hi, {user.firstname}! Great to meet you.

diff --git a/client/src/components/pages/Register/index.tsx b/client/src/components/pages/Register/index.tsx index 4d42422..bf39270 100644 --- a/client/src/components/pages/Register/index.tsx +++ b/client/src/components/pages/Register/index.tsx @@ -19,17 +19,17 @@ export enum VariantLabel { FinishUp } -const Register: FC<{receiveChange: (change: IUser) => void}> = ({ receiveChange }) => { +const Register = () => { const [displayed, setDisplayed] = useState(); - const authContext = useAuthContext(); + const { user } = useAuthContext(); - const transitionDisplay = (variant: number | VariantLabel, user?: IUser) => { + const transitionDisplay = (variant: number | VariantLabel) => { switch (variant) { case 0: setDisplayed(); break; case 1: - setDisplayed(); + setDisplayed(); break; case 2: setDisplayed(); @@ -38,7 +38,7 @@ const Register: FC<{receiveChange: (change: IUser) => void}> = ({ receiveChange setDisplayed(); break; default: - setDisplayed(); + setDisplayed(); break; } } diff --git a/client/src/components/ui/Navbar/index.tsx b/client/src/components/ui/Navbar/index.tsx index b15b1db..8e3aca1 100644 --- a/client/src/components/ui/Navbar/index.tsx +++ b/client/src/components/ui/Navbar/index.tsx @@ -1,44 +1,25 @@ -import { FC, useCallback, useEffect, useState } from "react"; -import { useNavigate } from "react-router-dom"; +import { useEffect, useState } from "react"; import { LoggedIn, NotLoggedIn, Registering } from "./variants"; import { useAuthContext } from "../../../context/AuthContext"; -import { IUser } from "../../../schemas"; import "/src/sass/components/Navbar.scss"; -const Navbar: FC<{receiveChange: (change: IUser) => void}> = ({ receiveChange }) => { +const Navbar = () => { // setup and local state - const navigate = useNavigate(); - const { user, setUser } = useAuthContext(); - const [received, setReceived] = useState(); - const [displayed, setDisplayed] = useState(); - - // lift and store state from navbar variants - const liftChange = useCallback((newValue: IUser | undefined) => { - if (!newValue) { - return; - } - - setUser(newValue); - setReceived(newValue); - }, []) + const { user } = useAuthContext(); + const [displayed, setDisplayed] = useState(

Loading...

); const variants = { - loggedin: , - notloggedin: , - registering: + loggedin: , + notloggedin: , + registering: } // side effects for live rendering useEffect(() => { - user && setReceived(user); - }, [user]) + setDisplayed(user ? variants.loggedin : variants.notloggedin); + }, [user]); - useEffect(() => { - if (received) receiveChange(received); - setDisplayed(received ? variants.loggedin : variants.notloggedin); - }, [received, setReceived]); - - return displayed ||

Loading...

; + return displayed; } export default Navbar; \ No newline at end of file diff --git a/client/src/components/ui/Navbar/variants.tsx b/client/src/components/ui/Navbar/variants.tsx index a9fa831..e76694c 100644 --- a/client/src/components/ui/Navbar/variants.tsx +++ b/client/src/components/ui/Navbar/variants.tsx @@ -1,15 +1,26 @@ -import { attemptLogout } from "../../../util/apiUtils"; +import API from "../../../util/API"; import { NavbarType } from "../../../util/types"; -import { Button, Dropdown } from '../.' +import { Button, Dropdown } from '..' import { useState } from "react"; +import { useAuthContext } from "../../../context/AuthContext"; +import { useNavigate } from "react-router-dom"; + +const LoggedIn = () => { + const { user, setUser, setToken } = useAuthContext(); + const navigate = useNavigate(); + const auth = new API.Auth(); -const LoggedIn: NavbarType = ({ received, liftChange, navigate }) => { const [dropdownActive, setDropdownActive] = useState(false); const [searchActive, setSearchActive] = useState(false); const handleLogout = async () => { - const success = await attemptLogout(); - if (success) liftChange!(undefined); + const success = await auth.logout(); + console.log(success); + + // nullify cookie and unset user/token data + document.cookie = `token=;expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`; + setUser(undefined); + setToken(undefined); navigate('/'); } @@ -36,7 +47,7 @@ const LoggedIn: NavbarType = ({ received, liftChange, navigate }) => { navigate('/')}>RECIPIN
-

Hi, {received?.firstname}.

+

Hi, {user?.firstname}.

@@ -64,7 +75,9 @@ const LoggedIn: NavbarType = ({ received, liftChange, navigate }) => { ) } -const NotLoggedIn: NavbarType = ({ navigate }) => { +const NotLoggedIn = () => { + const navigate = useNavigate(); + return (