46 lines
No EOL
1.1 KiB
TypeScript
46 lines
No EOL
1.1 KiB
TypeScript
"use client"
|
|
|
|
import React, { createContext, useContext, useEffect, useState } from 'react';
|
|
|
|
interface AuthContextProps {
|
|
isAuthenticated: boolean | null;
|
|
login: () => void;
|
|
logout: () => void;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextProps>({
|
|
isAuthenticated: null,
|
|
login: () => {},
|
|
logout: () => {},
|
|
});
|
|
|
|
export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
|
|
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
|
|
|
|
useEffect(() => {
|
|
const token = localStorage.getItem('token');
|
|
if (token) {
|
|
// You could add any token validation logic here
|
|
setIsAuthenticated(true);
|
|
} else {
|
|
setIsAuthenticated(false);
|
|
}
|
|
}, []);
|
|
|
|
const login = () => {
|
|
setIsAuthenticated(true);
|
|
};
|
|
|
|
const logout = () => {
|
|
setIsAuthenticated(false);
|
|
localStorage.removeItem('token');
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ isAuthenticated, login, logout }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useAuth = () => useContext(AuthContext); |