first commit
This commit is contained in:
77
frontend/src/hooks/useAuth.ts
Normal file
77
frontend/src/hooks/useAuth.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { apiRequest, setToken, clearToken } from '../api/client';
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
user: User | null;
|
||||
isLoading: boolean;
|
||||
isAuthenticated: boolean;
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
const [state, setState] = useState<AuthState>({
|
||||
user: null,
|
||||
isLoading: true,
|
||||
isAuthenticated: false,
|
||||
});
|
||||
|
||||
const checkAuth = useCallback(async () => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) {
|
||||
setState({ user: null, isLoading: false, isAuthenticated: false });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await apiRequest<User>('/auth/me');
|
||||
setState({ user, isLoading: false, isAuthenticated: true });
|
||||
} catch {
|
||||
clearToken();
|
||||
setState({ user: null, isLoading: false, isAuthenticated: false });
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
checkAuth();
|
||||
}, [checkAuth]);
|
||||
|
||||
const login = async (username: string, password: string) => {
|
||||
const { token, user } = await apiRequest<{ token: string; user: User }>(
|
||||
'/auth/login',
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ username, password }),
|
||||
}
|
||||
);
|
||||
setToken(token);
|
||||
setState({ user, isLoading: false, isAuthenticated: true });
|
||||
};
|
||||
|
||||
const register = async (username: string, password: string) => {
|
||||
const { token, user } = await apiRequest<{ token: string; user: User }>(
|
||||
'/auth/register',
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ username, password }),
|
||||
}
|
||||
);
|
||||
setToken(token);
|
||||
setState({ user, isLoading: false, isAuthenticated: true });
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
clearToken();
|
||||
setState({ user: null, isLoading: false, isAuthenticated: false });
|
||||
};
|
||||
|
||||
return {
|
||||
...state,
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user