29 lines
813 B
TypeScript
29 lines
813 B
TypeScript
import React, { createContext, useContext, useState, ReactNode } from 'react';
|
|
|
|
interface AuthContextType {
|
|
isAuthenticated: boolean;
|
|
login: () => void;
|
|
logout: () => void;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
|
|
export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
|
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
|
|
const login = () => setIsAuthenticated(true);
|
|
const logout = () => setIsAuthenticated(false);
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ isAuthenticated, login, logout }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useAuth = () => {
|
|
const context = useContext(AuthContext);
|
|
if (!context) throw new Error('useAuth must be used within AuthProvider');
|
|
return context;
|
|
};
|