- Updated the `AprovarFerias.svelte` component to use specific types for `solicitacao` and `gestorId`, enhancing type safety. - Improved error handling by refining catch blocks to handle errors more accurately. - Made minor adjustments to ensure consistent code formatting and readability across the component.
155 lines
3.5 KiB
TypeScript
155 lines
3.5 KiB
TypeScript
import { browser } from "$app/environment";
|
|
import { goto } from "$app/navigation";
|
|
import type { Id } from "@sgse-app/backend/convex/betterAuth/_generated/dataModel";
|
|
|
|
interface Usuario {
|
|
_id: string;
|
|
matricula: string;
|
|
nome: string;
|
|
email: string;
|
|
funcionarioId?: Id<"funcionarios">;
|
|
role: {
|
|
_id: string;
|
|
nome: string;
|
|
nivel: number;
|
|
setor?: string;
|
|
};
|
|
primeiroAcesso: boolean;
|
|
avatar?: string;
|
|
fotoPerfil?: string;
|
|
fotoPerfilUrl?: string | null;
|
|
}
|
|
|
|
interface AuthState {
|
|
usuario: Usuario | null;
|
|
token: string | null;
|
|
carregando: boolean;
|
|
}
|
|
|
|
class AuthStore {
|
|
private state = $state<AuthState>({
|
|
usuario: null,
|
|
token: null,
|
|
carregando: true,
|
|
});
|
|
|
|
constructor() {
|
|
if (browser) {
|
|
this.carregarDoLocalStorage();
|
|
}
|
|
}
|
|
|
|
get usuario() {
|
|
return this.state.usuario;
|
|
}
|
|
|
|
get token() {
|
|
return this.state.token;
|
|
}
|
|
|
|
get carregando() {
|
|
return this.state.carregando;
|
|
}
|
|
|
|
get autenticado() {
|
|
return !!this.state.usuario && !!this.state.token;
|
|
}
|
|
|
|
get isAdmin() {
|
|
return this.state.usuario?.role.nivel === 0;
|
|
}
|
|
|
|
get isTI() {
|
|
return this.state.usuario?.role.nome === "ti" || this.isAdmin;
|
|
}
|
|
|
|
get isRH() {
|
|
return this.state.usuario?.role.nome === "rh" || this.isAdmin;
|
|
}
|
|
|
|
login(usuario: Usuario, token: string) {
|
|
this.state.usuario = usuario;
|
|
this.state.token = token;
|
|
this.state.carregando = false;
|
|
|
|
if (browser) {
|
|
localStorage.setItem("auth_token", token);
|
|
localStorage.setItem("auth_usuario", JSON.stringify(usuario));
|
|
}
|
|
}
|
|
|
|
logout() {
|
|
this.state.usuario = null;
|
|
this.state.token = null;
|
|
this.state.carregando = false;
|
|
|
|
if (browser) {
|
|
localStorage.removeItem("auth_token");
|
|
localStorage.removeItem("auth_usuario");
|
|
goto("/");
|
|
}
|
|
}
|
|
|
|
setCarregando(carregando: boolean) {
|
|
this.state.carregando = carregando;
|
|
}
|
|
|
|
async refresh() {
|
|
if (!browser || !this.state.token) return;
|
|
|
|
try {
|
|
// Importação dinâmica do convex para evitar problemas de SSR
|
|
const { ConvexHttpClient } = await import("convex/browser");
|
|
const { api } = await import("@sgse-app/backend/convex/_generated/api");
|
|
|
|
const client = new ConvexHttpClient(import.meta.env.VITE_CONVEX_URL);
|
|
client.setAuth(this.state.token);
|
|
|
|
const usuarioAtualizado = await client.query(
|
|
api.usuarios.obterPerfil,
|
|
{}
|
|
);
|
|
|
|
if (usuarioAtualizado) {
|
|
// Preservar role e primeiroAcesso do estado atual
|
|
this.state.usuario = {
|
|
...usuarioAtualizado,
|
|
role: this.state.usuario?.role || {
|
|
_id: "",
|
|
nome: "Usuário",
|
|
nivel: 999,
|
|
},
|
|
primeiroAcesso: this.state.usuario?.primeiroAcesso ?? false,
|
|
};
|
|
|
|
localStorage.setItem(
|
|
"auth_usuario",
|
|
JSON.stringify(this.state.usuario)
|
|
);
|
|
}
|
|
} catch (error) {
|
|
console.error("Erro ao atualizar perfil:", error);
|
|
}
|
|
}
|
|
|
|
private carregarDoLocalStorage() {
|
|
const token = localStorage.getItem("auth_token");
|
|
const usuarioStr = localStorage.getItem("auth_usuario");
|
|
|
|
if (token && usuarioStr) {
|
|
try {
|
|
const usuario = JSON.parse(usuarioStr);
|
|
this.state.usuario = usuario;
|
|
this.state.token = token;
|
|
} catch (error) {
|
|
console.error("Erro ao carregar usuário do localStorage:", error);
|
|
this.logout();
|
|
}
|
|
}
|
|
|
|
this.state.carregando = false;
|
|
}
|
|
}
|
|
|
|
export const authStore = new AuthStore();
|