added server
This commit is contained in:
43
frontend/src/hooks/useAuth.ts
Normal file
43
frontend/src/hooks/useAuth.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import {useState, useEffect} from 'react';
|
||||
import type {User} from '@/types';
|
||||
|
||||
export const useAuth = () => {
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
// Check if user is logged in (from localStorage)
|
||||
const storedUser = localStorage.getItem('user');
|
||||
if (storedUser) {
|
||||
setUser(JSON.parse(storedUser));
|
||||
}
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
const login = (email: string, password: string) => {
|
||||
// Simple authentication (in production, use a real backend)
|
||||
if (email && password) {
|
||||
const newUser: User = {
|
||||
email,
|
||||
isAuthenticated: true
|
||||
};
|
||||
localStorage.setItem('user', JSON.stringify(newUser));
|
||||
setUser(newUser);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
localStorage.removeItem('user');
|
||||
setUser(null);
|
||||
};
|
||||
|
||||
return {
|
||||
user,
|
||||
loading,
|
||||
login,
|
||||
logout,
|
||||
isAuthenticated: !!user?.isAuthenticated
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user