add login feature

This commit is contained in:
2025-10-22 06:55:28 -04:00
parent f2cf309d97
commit 28d0443f44
22 changed files with 287 additions and 1136 deletions

View File

@@ -14,17 +14,38 @@ export const useAuth = () => {
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;
const login = async (username: string, password: string) => {
if (username && password) {
const response = await fetch('/api/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username,
password,
}),
});
if (!response.ok) {
return false;
}
const responseData = await response.json();
if (responseData.status === 'ok') {
setUser({username, isAuthenticated: true});
localStorage.setItem('user', JSON.stringify({username, isAuthenticated: true}));
return true;
} else {
setUser({username, isAuthenticated: false});
localStorage.setItem('user', JSON.stringify({username, isAuthenticated: false}));
return false;
}
}
setUser({username, isAuthenticated: false});
localStorage.setItem('user', JSON.stringify({username, isAuthenticated: false}));
return false;
};