Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Nov 22, 2023
1 parent ca28bb4 commit aef942a
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/utils/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import supabase from './supabase';

type AuthContextAction =
| { type: 'LOADING' }
| { type: 'CLEAR_ERROR' }
| { type: 'SIGN_UP' }
| { type: 'SIGN_IN_WITH_EMAIL' }
| { type: 'VERIFY_OTP' }
Expand All @@ -30,7 +29,7 @@ export type AuthDispatch = React.Dispatch<AuthContextAction>;

export interface AuthState {
session: Session | null;
user: User | null;
userProfile: User | null;
isLoading: boolean;
dispatch: AuthDispatch;
}
Expand All @@ -54,30 +53,28 @@ export const useAuthReducer = () =>
case 'SIGN_OUT':
return {
...prevState,
user: null,
userProfile: null,
session: null,
isLoading: false,
};
case 'REFRESH_SESSION':
return {
...prevState,
session: action.session,
user: action.session ? action.session?.user : null,
userProfile: action.session ? action.session?.user : null,
isLoading: false,
};
case 'LOADING':
console.log('loading');
return { ...prevState, isLoading: true };
case 'CLEAR_ERROR':
return { ...prevState, error: null };
default:
return prevState;
}
},
{
session: null,
isLoading: false,
user: null,
userProfile: null,
dispatch: () => null,
},
);
Expand All @@ -102,16 +99,15 @@ export function AuthContextProvider({
}) {
const [authState, dispatch] = useAuthReducer();

useEffect(() => {
console.log(`New auth session: ${JSON.stringify(authState)}`);
}, [authState]);

useEffect(() => {
supabase.auth.getSession().then(({ data: { session: newSession } }) => {
console.log('callback');

dispatch({ type: 'REFRESH_SESSION', session: newSession });
});

supabase.auth.onAuthStateChange((_event, newSession) => {
console.log('callback');
dispatch({ type: 'REFRESH_SESSION', session: newSession });
});
}, []);
Expand Down

0 comments on commit aef942a

Please sign in to comment.