-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from nepalcodes/auth-react-context
added Auth react context
- Loading branch information
Showing
14 changed files
with
257 additions
and
103 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
Empty file.
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,4 @@ | ||
"जैठा तक दमल ड छोक ऐठा तक जाईस, आगुबिती जलेसे दमाल ड ऐठा जाए हुना दख्बो","Walk as far as you can see the road, The way forward is visible after reaching there" | ||
"सफलतर सुरुवत सबै दिन आसफलतारसे हल्कु","Success always begins with failure" | ||
"जैला ठिक हो ऐला करिस, जैला सजिलो लगेसोक ऐल ना करिस","Do what is right, not what is easy" | ||
"जैला जीवन डक समयर महत्व रखेसेक ओईला आप्नाई आप्नाक जनेसेक","The one who has accepted the time in life has known himself" |
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,61 @@ | ||
import React, { | ||
createContext, | ||
useContext, | ||
useState, | ||
ReactNode, | ||
useEffect, | ||
} from "react"; | ||
import supabase from "./supabaseClient"; | ||
import { AuthError, AuthResponse, AuthTokenResponsePassword, SignInWithPasswordCredentials, SignUpWithPasswordCredentials, User } from "@supabase/supabase-js"; | ||
|
||
interface AuthContextProps { | ||
user: User | null; | ||
signUp: (data: SignUpWithPasswordCredentials) => Promise<AuthResponse>; | ||
signIn: (data: SignInWithPasswordCredentials) => Promise<AuthTokenResponsePassword>; | ||
signOut: () => Promise<{ error: AuthError | null }> | ||
} | ||
|
||
const AuthContext = createContext<AuthContextProps | undefined>(undefined); | ||
|
||
export const AuthProvider = ({ children }: { children: ReactNode }) => { | ||
const [user, setUser] = useState<User | null>(null); | ||
const [, setIsAuthenticated] = useState(false); | ||
|
||
useEffect(() => { | ||
const fetchSession = async () => { | ||
const { | ||
data: { session }, | ||
} = await supabase.auth.getSession(); | ||
setIsAuthenticated(!!session); | ||
setUser(session?.user ?? null); | ||
}; | ||
|
||
fetchSession(); | ||
|
||
const { | ||
data: { subscription }, | ||
} = supabase.auth.onAuthStateChange((_event, session) => { | ||
setIsAuthenticated(!!session); | ||
setUser(session?.user ?? null); | ||
}); | ||
|
||
return () => subscription.unsubscribe(); | ||
}, []); | ||
|
||
const value: AuthContextProps = { | ||
signUp: (data) => supabase.auth.signUp(data), | ||
signIn: (data) => supabase.auth.signInWithPassword(data), | ||
signOut: () => supabase.auth.signOut(), | ||
user, | ||
}; | ||
|
||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | ||
}; | ||
|
||
export const useAuth = () => { | ||
const context = useContext(AuthContext); | ||
if (context === undefined) { | ||
throw new Error("useAuth must be used within an AuthProvider"); | ||
} | ||
return context; | ||
}; |
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
Oops, something went wrong.