feat: add login page and auth guard

This commit is contained in:
2026-02-06 14:45:59 -03:00
parent 547619a1a7
commit 8b6e59d346
6 changed files with 185 additions and 7 deletions

35
dashboard/src/lib/auth.ts Normal file
View File

@@ -0,0 +1,35 @@
export function getToken(): string | null {
if (typeof window === 'undefined') return null;
return localStorage.getItem('token');
}
export function setToken(token: string) {
localStorage.setItem('token', token);
}
export function removeToken() {
localStorage.removeItem('token');
}
export function isAuthenticated(): boolean {
return !!getToken();
}
export async function fetchWithAuth(url: string, options: RequestInit = {}) {
const token = getToken();
const headers = {
...options.headers,
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
};
const res = await fetch(url, { ...options, headers });
if (res.status === 401) {
removeToken();
window.location.href = '/login';
throw new Error('Unauthorized');
}
return res;
}