From 84346e9a3c4a8a305edb8fe6dec623135e90a61c Mon Sep 17 00:00:00 2001 From: Nick Cherry Date: Wed, 31 Jan 2024 21:29:37 -0500 Subject: [PATCH] persist session --- mobile/src/components/auth/Login.tsx | 18 ++++++++++-------- mobile/src/contexts/AuthProvider.tsx | 26 +++++++++++++++----------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/mobile/src/components/auth/Login.tsx b/mobile/src/components/auth/Login.tsx index 13ead3e..d0f68ea 100644 --- a/mobile/src/components/auth/Login.tsx +++ b/mobile/src/components/auth/Login.tsx @@ -24,12 +24,16 @@ export function Login() { url, } = useSignIn({ onSuccess: useCallback( - (req: StatusAPIResponse) => { - signIn({ - message: req.message!, - nonce: req.nonce!, - signature: req.signature!, - }); + async (req: StatusAPIResponse) => { + try { + signIn({ + message: req.message!, + nonce: req.nonce!, + signature: req.signature!, + }); + } catch (error) { + alert((error as Error).message); + } }, [signIn], ), @@ -43,10 +47,8 @@ export function Login() { if (!hasInitiatedConnectRef.current) { hasInitiatedConnectRef.current = true; - console.log('connecting'); await connect(); } else if (isConnectError) { - console.log('reconnecting'); reconnect(); } }, [connect, isConnectError, reconnect]); diff --git a/mobile/src/contexts/AuthProvider.tsx b/mobile/src/contexts/AuthProvider.tsx index 44c1fc7..0cf0f82 100644 --- a/mobile/src/contexts/AuthProvider.tsx +++ b/mobile/src/contexts/AuthProvider.tsx @@ -32,8 +32,8 @@ type State = { }; type Action = - | { type: 'signIn'; session: Session; user: User } - | { type: 'signOut' }; + | { type: 'onSignIn'; session: Session; user: User } + | { type: 'onSignOut' }; const AuthContext = createContext<{ currentUser: User | undefined; @@ -55,13 +55,13 @@ const initialState: State = { function reducer(_state: State, action: Action): State { switch (action.type) { - case 'signIn': + case 'onSignIn': return { isInitialized: true, currentUser: action.user, session: action.session, }; - case 'signOut': + case 'onSignOut': return { isInitialized: true, session: undefined, @@ -83,6 +83,7 @@ function AuthProviderContent({ children }: AuthProviderProps) { const reject = (message: string) => { throw new Error(`Sign in failed: ${message}`); }; + try { const signInResponse = await fetch( 'http://localhost:3000/api/auth/sign-in', @@ -97,9 +98,9 @@ function AuthProviderContent({ children }: AuthProviderProps) { } const session: Session = await signInResponse.json(); - // await SecureStore.setItemAsync(sessionKey, JSON.stringify(session)); + await SecureStore.setItemAsync(sessionKey, JSON.stringify(session)); const { profile: user } = await fetchProfile({ fid: session.fid }); - dispatch({ type: 'signIn', session, user }); + dispatch({ type: 'onSignIn', session, user }); } catch (error) { reject((error as Error).message); } @@ -115,7 +116,7 @@ function AuthProviderContent({ children }: AuthProviderProps) { }); } - dispatch({ type: 'signOut' }); + dispatch({ type: 'onSignOut' }); }, [state.session]); const init = useCallback(async () => { @@ -123,14 +124,17 @@ function AuthProviderContent({ children }: AuthProviderProps) { if (persistedSessionJson) { try { - signIn(JSON.parse(persistedSessionJson)); + const session: Session = JSON.parse(persistedSessionJson); + const { profile: user } = await fetchProfile({ fid: session.fid }); + dispatch({ type: 'onSignIn', session, user }); } catch (error) { console.error(error); + dispatch({ type: 'onSignOut' }); } + } else { + dispatch({ type: 'onSignOut' }); } - - dispatch({ type: 'signOut' }); - }, [signIn]); + }, [fetchProfile]); useEffect(() => { init();