commerce/contexts/auth-context.tsx
Chloe 8333eb36fc
clean up unused code and create login callback api endpoints
Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
2024-06-22 15:09:35 +07:00

39 lines
883 B
TypeScript

'use client';
import { isLoggedIn } from 'components/profile/actions';
import { createContext, useEffect, useState } 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;