boredgame/src/hooks.server.ts

98 lines
3.3 KiB
TypeScript
Raw Normal View History

// import * as Sentry from '@sentry/sveltekit';
import 'reflect-metadata'
2024-07-21 19:05:48 +00:00
import { hc } from 'hono/client';
import { sequence } from '@sveltejs/kit/hooks';
2024-07-21 19:05:48 +00:00
import { redirect, type Handle } from '@sveltejs/kit';
import { dev } from '$app/environment';
import { lucia } from '$lib/server/auth';
2024-07-21 19:05:48 +00:00
import type { ApiRoutes } from '$lib/server/api';
import { parseApiResponse } from '$lib/utils/api';
import { StatusCodes } from '$lib/constants/status-codes';
// 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
// });
2023-05-21 05:18:04 +00:00
2024-07-21 19:05:48 +00:00
const apiClient: Handle = async ({ event, resolve }) => {
/* ------------------------------ Register api ------------------------------ */
const { api } = hc<ApiRoutes>('/', {
fetch: event.fetch,
headers: {
host: event.request.headers.get('host') || ''
}
});
/* ----------------------------- Auth functions ----------------------------- */
async function getAuthedUser() {
const { data } = await api.user.me.$get().then(parseApiResponse)
2024-07-21 19:05:48 +00:00
return data && data.user;
}
async function getAuthedUserOrThrow() {
const { data } = await api.user.me.$get().then(parseApiResponse);
2024-07-21 19:05:48 +00:00
if (!data || !data.user) throw redirect(StatusCodes.TEMPORARY_REDIRECT, '/');
return data?.user;
}
/* ------------------------------ Set contexts ------------------------------ */
event.locals.api = api;
event.locals.parseApiResponse = parseApiResponse;
event.locals.getAuthedUser = getAuthedUser;
event.locals.getAuthedUserOrThrow = getAuthedUserOrThrow;
/* ----------------------------- Return response ---------------------------- */
const response = await resolve(event);
return response;
};
export const authentication: Handle = async function ({ event, resolve }) {
2024-07-15 17:03:50 +00:00
event.locals.startTimer = Date.now();
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,
});
2023-05-21 05:18:04 +00:00
}
2024-03-11 06:19:55 +00:00
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, {
2024-03-11 06:19:55 +00:00
path: '.',
...sessionCookie.attributes,
});
}
event.locals.user = user;
event.locals.session = session;
return resolve(event);
};
export const handle: Handle = sequence(
// Sentry.sentryHandle(),
authentication,
2024-07-21 19:05:48 +00:00
apiClient
);
2024-03-11 06:19:55 +00:00
// export const handleError = Sentry.handleErrorWithSentry();