From 31c33447fbbc2618e09acf5032d0df0bf8f65d46 Mon Sep 17 00:00:00 2001 From: jwu-fisker Date: Wed, 6 Jan 2021 18:23:38 -0800 Subject: [PATCH] Refactor UserContext more async Separate signup and signin --- src/components/SignUpForm/index.jsx | 3 +- src/components/contexts/UserContext.jsx | 78 +++++++++++++------------ 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/components/SignUpForm/index.jsx b/src/components/SignUpForm/index.jsx index a6b838b..1e27a75 100644 --- a/src/components/SignUpForm/index.jsx +++ b/src/components/SignUpForm/index.jsx @@ -5,7 +5,7 @@ import useStyles from '../Styles'; import { useUserContext } from '../Contexts/UserContext'; export default function SignInForm() { - const { signUp, fetching, setError } = useUserContext(); + const { signUp, signIn, fetching, setError } = useUserContext(); const classes = useStyles(); const emailEl = useRef(null); const passwordEl = useRef(null); @@ -17,6 +17,7 @@ export default function SignInForm() { const password = passwordEl.current.value; const confirm = confirmEl.current.value; await signUp(email, password, confirm); + await signIn(email, password); } catch (e) { setError(e.message); diff --git a/src/components/contexts/UserContext.jsx b/src/components/contexts/UserContext.jsx index e0c11b3..b31bb6f 100644 --- a/src/components/contexts/UserContext.jsx +++ b/src/components/contexts/UserContext.jsx @@ -14,53 +14,57 @@ export const UserProvider = ({ children }) => { if (!token) return; const { accessToken: { jwtToken }} = token; const verifyToken = async (jwt) => { - debugger; - const result = await auth.verify(jwtToken); + const result = await auth.verify(jwt); if (result.authenticated) { setToken(token); } else { console.log(result); await signOut(); } - }; - verifyToken(jwtToken); + }; + verifyToken(); + return () => {}; }, []); + + const signIn = async (username, password) => { + try { + if (!username) throw new Error('Email is required'); + if (!password) throw new Error('Password is required'); + + setFetching(true); + setError(null); + + const result = await auth.signIn(username, password); - const signIn = (username, password) => { - if (!username) throw new Error('Email is required'); - if (!password) throw new Error('Password is required'); - setFetching(true); - setError(null); - return auth.signIn(username, password) - .then((result) => { - setFetching(false); - if (result.message) throw new Error(result.message); - signedIn(result); - return result; - }) - .catch((error) => { - setError(error.message); - setFetching(false); - return null; - }); + if (result.message) throw new Error(result.message); + signedIn(result); + } + catch (error) { + setError(error.message); + } + finally { + setFetching(false); + } }; - const signUp = (username, password, confirmPassword) => { - if (!username) throw new Error('Email is required'); - if (!password) throw new Error('Password is required'); - if (password !== confirmPassword) throw new Error('Passwords do not match'); - setFetching(true); - setError(null); - return auth.signUp(username, password) - .then((result) => { - if (result.message) throw new Error(result.message); - return signIn(username, password); - }) - .catch((error) => { - setError(error.message); - setFetching(false); - return null; - }); + const signUp = async (username, password, confirmPassword) => { + try { + if (!username) throw new Error('Email is required'); + if (!password) throw new Error('Password is required'); + if (password !== confirmPassword) throw new Error('Passwords do not match'); + + setFetching(true); + setError(null); + + const result = await auth.signUp(username, password); + if (result.message) throw new Error(result.message); + } + catch (error) { + setError(error.message); + } + finally { + setFetching(false); + } }; const signOut = async () => {