From 651605198ab2e329cc3115265ba78e0663cec2e5 Mon Sep 17 00:00:00 2001 From: Philip Ye Date: Sun, 21 Apr 2024 15:56:54 -0700 Subject: [PATCH] hopefully nothing blows up --- .../(Authentication)/Login/Password/index.tsx | 2 +- .../OTPFlow/OTPVerify/index.tsx | 5 +- src/context/AuthContext.tsx | 66 +++++++++++++------ 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/app/(Authentication)/Login/Password/index.tsx b/src/app/(Authentication)/Login/Password/index.tsx index 3148f9ac..b896f129 100644 --- a/src/app/(Authentication)/Login/Password/index.tsx +++ b/src/app/(Authentication)/Login/Password/index.tsx @@ -22,7 +22,7 @@ export default function LoginScreen() { }; async function signIn() { - const { error } = await sessionHandler.signInWithEmail(email, password); + const error = await sessionHandler.signInWithEmail(email, password); console.log(error); if (error != null) { setErrorExists(true); diff --git a/src/app/(Authentication)/OTPFlow/OTPVerify/index.tsx b/src/app/(Authentication)/OTPFlow/OTPVerify/index.tsx index 0836cf1b..7ab364e0 100644 --- a/src/app/(Authentication)/OTPFlow/OTPVerify/index.tsx +++ b/src/app/(Authentication)/OTPFlow/OTPVerify/index.tsx @@ -30,8 +30,9 @@ export default function OTPFlow() { }; const verifyToken = async (token: string) => { - const { error } = await verifyOtp(email, token); - if (error) { + const err = await verifyOtp(email, token); + console.log(err); + if (err.message) { setErrorExists(true); setErrorMessage('Sorry! The verification code was incorrect.'); return; diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index ee5113bf..5eaabd7a 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -35,7 +35,7 @@ export interface AuthState { password: string, options: object, ) => Promise; - signInWithEmail: (email: string, password: string) => Promise; + signInWithEmail: (email: string, password: string) => Promise; fullySignUpUser: ( fullName: string, email: string, @@ -46,7 +46,7 @@ export interface AuthState { zip: string, ) => Promise; sendOtp: (email: string) => Promise; - verifyOtp: (email: string, token: string) => Promise; + verifyOtp: (email: string, token: string) => Promise; resendOtp: (email: string) => Promise; updateUser: (attributes: UserAttributes) => Promise; resetPassword: (email: string) => Promise< @@ -102,12 +102,20 @@ export function AuthContextProvider({ }; const signInWithEmail = async (email: string, password: string) => { - const value = await supabase.auth.signInWithPassword({ - email, - password, - }); // will trigger the use effect to update the session - setUser(value.data.user); - return value; + try { + const value = await supabase.auth.signInWithPassword({ + email, + password, + }); // will trigger the use effect to update the session + if (value.error) { + throw value.error; + } + setUser(value.data.user); + return null; + } catch (error) { + console.warn(error); + return error; + } }; const signUp = async (email: string, password: string, metaData: object) => { @@ -154,11 +162,14 @@ export function AuthContextProvider({ fullName, }); } + if (value.error) { + throw value.error; + } return value; } catch (error) { // eslint-disable-next-line no-console console.error('(signUpUser)', error); - throw error; + return error; } }; @@ -170,33 +181,46 @@ export function AuthContextProvider({ const sendOtp = async (email: string) => { try { - return await supabase.auth.signInWithOtp({ email }); + const value = await supabase.auth.signInWithOtp({ email }); + if (value.error) { + throw value.error; + } + return value; } catch (error) { console.warn('there was an error sending your one time passcode'); - throw error; + return error; } }; const verifyOtp = async (email: string, token: string) => { - const value = await supabase.auth.verifyOtp({ - email, - token, - type: 'email', - }); - - // if (value.data.user) setUser(value.data.user); - return value; + try { + const value = await supabase.auth.verifyOtp({ + email, + token, + type: 'email', + }); + if (value.error) { + throw value.error; + } + return null; + } catch (error) { + console.warn('otp not working uh oh'); + return error; + } }; const resendOtp = async (email: string) => { try { - return await supabase.auth.resend({ + const value = await supabase.auth.resend({ type: 'signup', email, }); + if (value.error) { + return value.error; + } } catch (error) { console.warn('there was an error resending your one time passcode'); - throw error; + return error; } };