Feat: Cambios Varios

This commit is contained in:
2025-12-23 15:12:57 -03:00
parent 32663e6324
commit 8bc1308bc5
58 changed files with 4080 additions and 663 deletions

View File

@@ -2,20 +2,36 @@ import { create } from 'zustand';
interface AuthState {
token: string | null;
role: string | null;
isAuthenticated: boolean;
setToken: (token: string) => void;
logout: () => void;
}
// Helper para decodificar el JWT sin librerías externas
const parseJwt = (token: string) => {
try {
return JSON.parse(atob(token.split('.')[1]));
} catch (e) {
return null;
}
};
export const useAuthStore = create<AuthState>((set) => ({
token: localStorage.getItem('token'),
role: localStorage.getItem('role'),
isAuthenticated: !!localStorage.getItem('token'),
setToken: (token: string) => {
const payload = parseJwt(token);
const role = payload["http://schemas.microsoft.com/ws/2008/06/identity/claims/role"];
localStorage.setItem('token', token);
set({ token, isAuthenticated: true });
localStorage.setItem('role', role);
set({ token, role, isAuthenticated: true });
},
logout: () => {
localStorage.removeItem('token');
set({ token: null, isAuthenticated: false });
localStorage.removeItem('role');
set({ token: null, role: null, isAuthenticated: false });
},
}));
}));