2024-01-31 01:57:36 +00:00
|
|
|
// import * as Sentry from '@sentry/sveltekit';
|
2024-08-01 23:46:29 +00:00
|
|
|
import 'reflect-metadata'
|
2024-07-21 19:05:48 +00:00
|
|
|
import { hc } from 'hono/client';
|
2023-06-16 06:28:49 +00:00
|
|
|
import { sequence } from '@sveltejs/kit/hooks';
|
2024-07-21 19:05:48 +00:00
|
|
|
import { redirect, type Handle } from '@sveltejs/kit';
|
2024-01-31 01:57:36 +00:00
|
|
|
import { dev } from '$app/environment';
|
2023-12-15 01:53:15 +00:00
|
|
|
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';
|
2023-05-29 06:34:39 +00:00
|
|
|
|
2024-01-31 01:57:36 +00:00
|
|
|
// 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: {
|
|
|
|
|
'x-forwarded-for': event.getClientAddress(),
|
|
|
|
|
host: event.request.headers.get('host') || ''
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* ----------------------------- Auth functions ----------------------------- */
|
|
|
|
|
async function getAuthedUser() {
|
|
|
|
|
const { data } = await api.iam.user.$get().then(parseApiResponse)
|
|
|
|
|
return data && data.user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getAuthedUserOrThrow() {
|
|
|
|
|
const { data } = await api.iam.user.$get().then(parseApiResponse);
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-16 06:28:49 +00:00
|
|
|
export const authentication: Handle = async function ({ event, resolve }) {
|
2024-07-15 17:03:50 +00:00
|
|
|
event.locals.startTimer = Date.now();
|
2023-05-29 06:34:39 +00:00
|
|
|
|
2024-01-19 00:57:15 +00:00
|
|
|
const ip = event.request.headers.get('x-forwarded-for') as string;
|
|
|
|
|
const country = event.request.headers.get('x-vercel-ip-country') as string;
|
2024-01-23 22:07:43 +00:00
|
|
|
event.locals.ip = dev ? '127.0.0.1' : ip; // || event.getClientAddress();
|
2024-01-19 00:57:15 +00:00
|
|
|
event.locals.country = dev ? 'us' : country;
|
2023-12-15 01:53:15 +00:00
|
|
|
|
|
|
|
|
const sessionId = event.cookies.get(lucia.sessionCookieName);
|
|
|
|
|
if (!sessionId) {
|
|
|
|
|
event.locals.user = null;
|
2024-01-19 00:57:15 +00:00
|
|
|
event.locals.session = null;
|
2023-12-15 01:53:15 +00:00
|
|
|
return resolve(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { session, user } = await lucia.validateSession(sessionId);
|
|
|
|
|
if (session && session.fresh) {
|
|
|
|
|
const sessionCookie = lucia.createSessionCookie(session.id);
|
2024-01-19 00:57:15 +00:00
|
|
|
console.log('sessionCookie', JSON.stringify(sessionCookie, null, 2));
|
2024-06-08 22:09:21 +00:00
|
|
|
// sveltekit types deviates from the de-facto standard, you can use 'as any' too
|
2024-01-19 00:57:15 +00:00
|
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
|
|
|
path: '.',
|
2024-06-08 22:09:21 +00:00
|
|
|
...sessionCookie.attributes,
|
2024-01-19 00:57:15 +00:00
|
|
|
});
|
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));
|
2023-12-15 01:53:15 +00:00
|
|
|
if (!session) {
|
|
|
|
|
const sessionCookie = lucia.createBlankSessionCookie();
|
2024-01-19 00:57:15 +00:00
|
|
|
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: '.',
|
2024-06-08 22:09:21 +00:00
|
|
|
...sessionCookie.attributes,
|
2024-01-19 00:57:15 +00:00
|
|
|
});
|
2023-12-15 01:53:15 +00:00
|
|
|
}
|
|
|
|
|
event.locals.user = user;
|
2024-01-19 00:57:15 +00:00
|
|
|
event.locals.session = session;
|
2023-12-15 01:53:15 +00:00
|
|
|
|
|
|
|
|
return resolve(event);
|
2023-10-17 09:28:53 +00:00
|
|
|
};
|
|
|
|
|
|
2024-01-31 01:57:36 +00:00
|
|
|
export const handle: Handle = sequence(
|
|
|
|
|
// Sentry.sentryHandle(),
|
2024-06-08 22:09:21 +00:00
|
|
|
authentication,
|
2024-07-21 19:05:48 +00:00
|
|
|
apiClient
|
2024-01-31 01:57:36 +00:00
|
|
|
);
|
2024-03-11 06:19:55 +00:00
|
|
|
// export const handleError = Sentry.handleErrorWithSentry();
|