mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
// import * as Sentry from '@sentry/sveltekit';
|
|
import { sequence } from '@sveltejs/kit/hooks';
|
|
import type { Handle } from '@sveltejs/kit';
|
|
import { dev } from '$app/environment';
|
|
import { lucia } from '$lib/server/auth';
|
|
|
|
// TODO: Fix Sentry as it is not working on SvelteKit v2
|
|
// Sentry.init({
|
|
// dsn: 'https://742e43279df93a3c4a4a78c12eb1f879@o4506057768632320.ingest.sentry.io/4506057770401792',
|
|
// tracesSampleRate: 1,
|
|
// environment: dev ? 'development' : 'production',
|
|
// enabled: !dev
|
|
// });
|
|
|
|
export const authentication: Handle = async function ({ event, resolve }) {
|
|
const startTimer = Date.now();
|
|
event.locals.startTimer = startTimer;
|
|
|
|
const ip = event.request.headers.get('x-forwarded-for') as string;
|
|
const country = event.request.headers.get('x-vercel-ip-country') as string;
|
|
event.locals.ip = dev ? '127.0.0.1' : ip; // || event.getClientAddress();
|
|
event.locals.country = dev ? 'us' : country;
|
|
|
|
const sessionId = event.cookies.get(lucia.sessionCookieName);
|
|
if (!sessionId) {
|
|
event.locals.user = null;
|
|
event.locals.session = null;
|
|
return resolve(event);
|
|
}
|
|
|
|
const { session, user } = await lucia.validateSession(sessionId);
|
|
if (session && session.fresh) {
|
|
const sessionCookie = lucia.createSessionCookie(session.id);
|
|
console.log('sessionCookie', JSON.stringify(sessionCookie, null, 2));
|
|
// sveltekit types deviates from the de-facto standard
|
|
// you can use 'as any' too
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
path: '.',
|
|
...sessionCookie.attributes
|
|
});
|
|
}
|
|
console.log('session from hooks', JSON.stringify(session, null, 2));
|
|
if (!session) {
|
|
const sessionCookie = lucia.createBlankSessionCookie();
|
|
console.log('blank sessionCookie', JSON.stringify(sessionCookie, null, 2));
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
path: '.',
|
|
...sessionCookie.attributes
|
|
});
|
|
}
|
|
event.locals.user = user;
|
|
event.locals.session = session;
|
|
|
|
return resolve(event);
|
|
};
|
|
|
|
export const handle: Handle = sequence(
|
|
// Sentry.sentryHandle(),
|
|
authentication
|
|
);
|
|
// export const handleError = Sentry.handleErrorWithSentry();
|