From 1a7ad4c2a270e1f69058131cf5db93eba1e77832 Mon Sep 17 00:00:00 2001 From: jwu-fisker Date: Fri, 8 Jan 2021 15:06:19 -0800 Subject: [PATCH] Fix sign up form bug --- src/components/Contexts/UserContext.jsx | 31 +++++++++++++++++-- src/components/Contexts/UserContext.test.jsx | 7 +++-- .../Contexts/__mocks__/UserContext.jsx | 15 +++++++-- src/components/SignUpForm/SignUpForm.test.jsx | 11 ++++--- src/components/SignUpForm/index.jsx | 5 ++- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/components/Contexts/UserContext.jsx b/src/components/Contexts/UserContext.jsx index 0fa235a..af42fa3 100644 --- a/src/components/Contexts/UserContext.jsx +++ b/src/components/Contexts/UserContext.jsx @@ -26,6 +26,8 @@ export const UserProvider = ({ children }) => { }, []); const signIn = async (username, password) => { + let result = null; + try { if (!username) throw new Error('Email is required'); if (!password) throw new Error('Password is required'); @@ -33,7 +35,7 @@ export const UserProvider = ({ children }) => { setFetching(true); setError(null); - const result = await auth.signIn(username, password); + result = await auth.signIn(username, password); if (result.message) throw new Error(result.message); signedIn(result); @@ -44,9 +46,13 @@ export const UserProvider = ({ children }) => { finally { setFetching(false); } + + return result; }; const signUp = async (username, password, confirmPassword) => { + let result = null; + try { if (!username) throw new Error('Email is required'); if (!password) throw new Error('Password is required'); @@ -55,7 +61,7 @@ export const UserProvider = ({ children }) => { setFetching(true); setError(null); - const result = await auth.signUp(username, password); + result = await auth.signUp(username, password); if (result.message) throw new Error(result.message); } catch (error) { @@ -64,6 +70,26 @@ export const UserProvider = ({ children }) => { finally { setFetching(false); } + + return result; + }; + + const signUpAndIn = async (username, password, confirmPassword) => { + let result = null; + + try { + result = await signUp(username, password, confirmPassword); + if (result.message) throw new Error(result.message); + result = await signIn(username, password); + } + catch (error) { + setError(error.message); + } + finally { + setFetching(false); + } + + return result; }; const signOut = async () => { @@ -86,6 +112,7 @@ export const UserProvider = ({ children }) => { setError, signIn, signUp, + signUpAndIn, signOut, }}> {children} diff --git a/src/components/Contexts/UserContext.test.jsx b/src/components/Contexts/UserContext.test.jsx index 0e773a3..c46bf60 100644 --- a/src/components/Contexts/UserContext.test.jsx +++ b/src/components/Contexts/UserContext.test.jsx @@ -19,7 +19,7 @@ describe("UseContext", () => {