Created
August 8, 2022 11:38
-
-
Save zinedkaloc/21b77c88a154c399605f7850560d6613 to your computer and use it in GitHub Desktop.
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
import React, { useContext, useState, useEffect } from 'react'; | |
import { altogic } from '../helpers/altogic'; | |
const AuthContext = React.createContext(); | |
export function useAuth() { | |
return useContext(AuthContext); | |
} | |
export function AuthProvider({ children }) { | |
const [user, setUser] = useState(); | |
const [session, setSession] = useState(); | |
const [loading, setLoading] = useState(true); | |
useEffect(() => { | |
// Check active sessions and sets the user and session | |
const session = altogic.auth.getSession(); | |
const user = altogic.auth.getUser(); | |
setUser(user ?? null); | |
setSession(session ?? null); | |
setLoading(false); | |
}, []); | |
// Will be passed down to Signup, Login, and Home components | |
const value = { | |
user, | |
session, | |
setUser, | |
setSession, | |
loading, | |
setLoading, | |
}; | |
return ( | |
<AuthContext.Provider value={value}> | |
{!loading && children} | |
</AuthContext.Provider> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment