- Updated the condition to safely check for the presence of user data in event.locals, preventing potential runtime errors when accessing nested properties.
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { getSessionData } from '$lib/utils/services/auth/session';
|
|
import { getHasCioCookies } from '$lib/utils/functions/cookies';
|
|
import { applyCspExtensions } from '$lib/utils/csp';
|
|
import { type Handle, redirect } from '@sveltejs/kit';
|
|
import { isPublicApiRoute, isPublicRoute } from '$lib/utils/functions/routes/isPublicRoute';
|
|
import { ROUTE } from '$lib/utils/constants/routes';
|
|
|
|
export const handle: Handle = async (args) => {
|
|
const { event } = args;
|
|
const sessionData = await getSessionData(event.cookies);
|
|
|
|
if (sessionData) {
|
|
event.locals = sessionData;
|
|
}
|
|
|
|
let response: Response;
|
|
|
|
if (event.url.pathname.includes('/api')) {
|
|
response = await handleAPIRoutes(args);
|
|
} else {
|
|
response = await handlePagesRoutes(args);
|
|
}
|
|
|
|
return applyCspExtensions(response);
|
|
};
|
|
|
|
const handlePagesRoutes: Handle = async ({ event, resolve }) => {
|
|
const { pathname } = event.url;
|
|
const hasCioCookie = getHasCioCookies(event.cookies);
|
|
|
|
if (isPublicRoute(pathname)) {
|
|
return resolve(event);
|
|
}
|
|
|
|
if (!event?.locals?.user && !hasCioCookie) {
|
|
console.log('no user and no cio cookie, redirecting to login');
|
|
const shouldAddRedirectParam = !pathname.includes(ROUTE.LOGOUT);
|
|
const fullPath = pathname + event.url.search;
|
|
const redirectPath = shouldAddRedirectParam
|
|
? `${ROUTE.LOGIN}?redirect=${encodeURIComponent(fullPath)}`
|
|
: ROUTE.LOGIN;
|
|
|
|
return redirect(303, redirectPath);
|
|
}
|
|
|
|
return resolve(event);
|
|
};
|
|
|
|
const handleAPIRoutes: Handle = async ({ event, resolve }) => {
|
|
const { pathname } = event.url;
|
|
|
|
if (isPublicApiRoute(pathname)) {
|
|
return resolve(event);
|
|
}
|
|
|
|
if (!event.locals.user) {
|
|
redirect(303, '/login');
|
|
}
|
|
|
|
return resolve(event);
|
|
};
|