feat: redesign superadmin agencies list, implement flat design, add date filters, and fix UI bugs
This commit is contained in:
79
front-end-agency/lib/auth.ts
Normal file
79
front-end-agency/lib/auth.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Auth utilities - Gerenciamento de autenticação no cliente
|
||||
*/
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string;
|
||||
role: string;
|
||||
tenantId?: string;
|
||||
company?: string;
|
||||
subdomain?: string;
|
||||
}
|
||||
|
||||
const TOKEN_KEY = 'token';
|
||||
const USER_KEY = 'user';
|
||||
|
||||
/**
|
||||
* Salva token e dados do usuário no localStorage
|
||||
*/
|
||||
export function saveAuth(token: string, user: User): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
localStorage.setItem(TOKEN_KEY, token);
|
||||
localStorage.setItem(USER_KEY, JSON.stringify(user));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna o token JWT armazenado
|
||||
*/
|
||||
export function getToken(): string | null {
|
||||
if (typeof window === 'undefined') return null;
|
||||
return localStorage.getItem(TOKEN_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna os dados do usuário armazenados
|
||||
*/
|
||||
export function getUser(): User | null {
|
||||
if (typeof window === 'undefined') return null;
|
||||
|
||||
const userStr = localStorage.getItem(USER_KEY);
|
||||
if (!userStr) return null;
|
||||
|
||||
try {
|
||||
return JSON.parse(userStr);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica se o usuário está autenticado
|
||||
*/
|
||||
export function isAuthenticated(): boolean {
|
||||
return !!getToken() && !!getUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove token e dados do usuário (logout)
|
||||
*/
|
||||
export function clearAuth(): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
localStorage.removeItem(USER_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna headers com Authorization para requisições autenticadas
|
||||
*/
|
||||
export function getAuthHeaders(): HeadersInit {
|
||||
const token = getToken();
|
||||
|
||||
return {
|
||||
'Content-Type': 'application/json',
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user