boredgame/src/hooks.server.ts

97 lines
3.5 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 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: {
2024-08-05 16:53:04 +00:00
'x-forwarded-for': event.url.host.includes('sveltekit-prerender') ? '127.0.0.1' : event.getClientAddress(),
2024-07-21 19:05:48 +00:00
host: event.request.headers.get('host') || ''
}
});
/* ----------------------------- Auth functions ----------------------------- */
async function getAuthedUser() {
2024-08-08 19:38:17 +00:00
const { data } = await api.user.$get().then(parseApiResponse)
return data?.user;
2024-07-21 19:05:48 +00:00
}
async function getAuthedUserOrThrow() {
2024-08-08 19:38:17 +00:00
const { data } = await api.user.$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;
};
2024-08-15 23:46:58 +00:00
// export const authentication: Handle = async function ({ event, resolve }) {
// 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,
// });
// }
// 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,
2024-07-21 19:05:48 +00:00
apiClient
);
2024-03-11 06:19:55 +00:00
// export const handleError = Sentry.handleErrorWithSentry();