-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[auth] Add auth context and useSession hook (#17)
* Add AuthContext * Add useSession hook in authentication components * Run formatter * Fix tsc errors --------- Co-authored-by: Aditya Pawar <[email protected]>
- Loading branch information
1 parent
e30c936
commit e24a4f3
Showing
6 changed files
with
117 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { AuthResponse, Session } from '@supabase/supabase-js'; | ||
import React, { | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import supabase from './supabase'; | ||
|
||
export interface AuthState { | ||
session: Session | null; | ||
signIn: (newSession: Session | null) => void; | ||
signUp: (email: string, password: string) => Promise<AuthResponse>; | ||
signInWithEmail: (email: string, password: string) => Promise<AuthResponse>; | ||
signOut: () => void; | ||
} | ||
|
||
const AuthContext = createContext({} as AuthState); | ||
|
||
export function useSession() { | ||
const value = useContext(AuthContext); | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (!value) { | ||
throw new Error( | ||
'useSession must be wrapped in a <AuthContextProvider />', | ||
); | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
|
||
export function AuthContextProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const [session, setSession] = useState<Session | null>(null); | ||
|
||
useEffect(() => { | ||
supabase.auth.getSession().then(({ data: { session: newSession } }) => { | ||
setSession(newSession); | ||
}); | ||
|
||
supabase.auth.onAuthStateChange((_event, newSession) => { | ||
setSession(newSession); | ||
}); | ||
}, []); | ||
|
||
const signIn = (newSession: Session | null) => { | ||
setSession(newSession); | ||
}; | ||
|
||
const signInWithEmail = async (email: string, password: string) => | ||
supabase.auth.signInWithPassword({ | ||
email, | ||
password, | ||
}); // will trigger the use effect to update the session | ||
const signUp = async (email: string, password: string) => | ||
supabase.auth.signUp({ | ||
email, | ||
password, | ||
}); // will trigger the use effect to update the session | ||
const signOut = () => { | ||
supabase.auth.signOut(); | ||
setSession(null); | ||
}; | ||
|
||
const authContextValue = useMemo( | ||
() => ({ | ||
session, | ||
signUp, | ||
signIn, | ||
signInWithEmail, | ||
signOut, | ||
}), | ||
[session], | ||
); | ||
|
||
return ( | ||
<AuthContext.Provider value={authContextValue}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
} |