Files
plantillas-proyectos/frontend/src/hooks.server.ts
AlexeerCT a5b378ccff refactor: centralize access token handling in cookies and improve related functions
- Introduced utility functions for managing access tokens in cookies, including setting, getting, and clearing tokens.
- Updated various components and server routes to utilize the new access token functions for better consistency and maintainability.
- Removed redundant cookie handling code across the application, streamlining the authentication process.
2026-05-01 21:30:52 -05:00

26 lines
894 B
TypeScript

import type { Handle } from '@sveltejs/kit';
import { paraglideMiddleware } from '$lib/paraglide/server';
import { sequence } from '@sveltejs/kit/hooks';
import { getAccessTokenFromCookies } from '$lib/server/access-token-cookie';
const handleParaglide: Handle = ({ event, resolve }) => paraglideMiddleware(event.request, ({ request, locale }) => {
event.request = request;
return resolve(event, {
transformPageChunk: ({ html }) => html.replace('%paraglide.lang%', locale)
});
});
const handleAuth: Handle = async ({ event, resolve }) => {
// Obtener el token de las cookies
const token = getAccessTokenFromCookies(event.cookies);
// Agregar el token a los locals para que esté disponible en toda la app
event.locals.token = token || null;
event.locals.isAuthenticated = !!token;
return resolve(event);
};
export const handle: Handle = sequence(handleAuth, handleParaglide);