podzahr/lib/AdminContext.tsx
nfel 9a7e627329
main: second iter
Signed-off-by: nfel <nfilsaraee@gmail.com>
2025-12-27 22:41:36 +03:30

55 lines
1.4 KiB
TypeScript

'use client';
import { createContext, useContext, useState, useEffect, ReactNode } from 'react';
interface AdminContextType {
isAuthenticated: boolean;
login: (username: string, password: string) => boolean;
logout: () => void;
}
const AdminContext = createContext<AdminContextType | undefined>(undefined);
// Demo credentials - In production, use proper authentication
const ADMIN_USERNAME = 'admin';
const ADMIN_PASSWORD = 'admin123';
export function AdminProvider({ children }: { children: ReactNode }) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
useEffect(() => {
const authStatus = localStorage.getItem('adminAuth');
if (authStatus === 'true') {
setIsAuthenticated(true);
}
}, []);
const login = (username: string, password: string): boolean => {
if (username === ADMIN_USERNAME && password === ADMIN_PASSWORD) {
setIsAuthenticated(true);
localStorage.setItem('adminAuth', 'true');
return true;
}
return false;
};
const logout = () => {
setIsAuthenticated(false);
localStorage.removeItem('adminAuth');
};
return (
<AdminContext.Provider value={{ isAuthenticated, login, logout }}>
{children}
</AdminContext.Provider>
);
}
export function useAdmin() {
const context = useContext(AdminContext);
if (context === undefined) {
throw new Error('useAdmin must be used within an AdminProvider');
}
return context;
}