43 lines
915 B
JavaScript
43 lines
915 B
JavaScript
"use client";
|
|
|
|
import { createContext, useContext, useEffect, useState } from "react";
|
|
import { account } from "../lib/appwrite";
|
|
|
|
const AuthContext = createContext();
|
|
|
|
export const AuthProvider = ({ children }) => {
|
|
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
const [authLoading, setAuthLoading] = useState(true);
|
|
|
|
const checkUserSession = async () => {
|
|
try {
|
|
const user = await account.get();
|
|
if (user) {
|
|
setIsAuthenticated(true);
|
|
}
|
|
} catch (error) {
|
|
setIsAuthenticated(false);
|
|
} finally {
|
|
setAuthLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
checkUserSession();
|
|
}, []);
|
|
|
|
return (
|
|
<AuthContext.Provider
|
|
value={{
|
|
isAuthenticated,
|
|
setIsAuthenticated,
|
|
loading: authLoading,
|
|
}}
|
|
>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useAuth = () => useContext(AuthContext);
|