Skip to content

Commit

Permalink
persist session
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcherry committed Feb 1, 2024
1 parent c2852ae commit 84346e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
18 changes: 10 additions & 8 deletions mobile/src/components/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
),
Expand All @@ -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]);
Expand Down
26 changes: 15 additions & 11 deletions mobile/src/contexts/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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);
}
Expand All @@ -115,22 +116,25 @@ function AuthProviderContent({ children }: AuthProviderProps) {
});
}

dispatch({ type: 'signOut' });
dispatch({ type: 'onSignOut' });
}, [state.session]);

const init = useCallback(async () => {
const persistedSessionJson = await SecureStore.getItemAsync(sessionKey);

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();
Expand Down

0 comments on commit 84346e9

Please sign in to comment.