mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
add useAuth context
This commit is contained in:
38
contexts/auth-context.tsx
Normal file
38
contexts/auth-context.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
'use client';
|
||||
import { isLoggedIn } from 'components/auth/actions';
|
||||
import { createContext, useState, useEffect } from 'react';
|
||||
|
||||
type AuthContextType = {
|
||||
isAuthenticated: boolean;
|
||||
loading: boolean;
|
||||
};
|
||||
|
||||
const AuthContext = createContext<AuthContextType>({
|
||||
isAuthenticated: false,
|
||||
loading: true
|
||||
});
|
||||
|
||||
type AuthProviderProps = {
|
||||
children: React.ReactNode;
|
||||
};
|
||||
|
||||
export function AuthProvider({ children }: AuthProviderProps) {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
async function checkAuth() {
|
||||
const isLogged = await isLoggedIn();
|
||||
setIsAuthenticated(isLogged);
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ isAuthenticated, loading }}>{children}</AuthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export default AuthContext;
|
Reference in New Issue
Block a user