lasthourcloud/src/hooks.ts

81 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-02-10 14:47:44 +00:00
import dotEnvExtended from 'dotenv-extended';
dotEnvExtended.load();
import type { GetSession } from '@sveltejs/kit';
import { handleSession } from 'svelte-kit-cookie-session';
import { getUserDetails, sentry } from '$lib/common';
2022-02-10 14:47:44 +00:00
import { version } from '$lib/common';
import cookie from 'cookie';
import { dev } from '$app/env';
export const handle = handleSession(
{
secret: process.env['COOLIFY_SECRET_KEY'],
2022-02-16 16:13:24 +00:00
expires: 30,
cookie: { secure: false }
2022-02-10 14:47:44 +00:00
},
async function ({ event, resolve }) {
let response;
try {
2022-02-18 20:37:40 +00:00
if (event.locals.cookies) {
if (event.locals.cookies['kit.session']) {
const { permission, teamId, userId } = await getUserDetails(event, false);
const newSession = {
userId,
teamId,
permission,
isAdmin: permission === 'admin' || permission === 'owner',
expires: event.locals.session.data.expires
2022-02-18 20:37:40 +00:00
};
2022-02-18 20:37:40 +00:00
if (JSON.stringify(event.locals.session.data) !== JSON.stringify(newSession)) {
event.locals.session.data = { ...newSession };
}
}
2022-02-10 14:47:44 +00:00
}
2022-02-10 14:47:44 +00:00
response = await resolve(event, {
ssr: !event.url.pathname.startsWith('/webhooks/success')
});
} catch (error) {
console.log(error);
2022-02-10 14:47:44 +00:00
response = await resolve(event, {
ssr: !event.url.pathname.startsWith('/webhooks/success')
});
response.headers.append(
'Set-Cookie',
cookie.serialize('kit.session', '', {
path: '/',
expires: new Date('Thu, 01 Jan 1970 00:00:01 GMT')
})
);
response.headers.append(
'Set-Cookie',
cookie.serialize('teamId', '', {
path: '/',
expires: new Date('Thu, 01 Jan 1970 00:00:01 GMT')
})
);
response.headers.append(
'Set-Cookie',
cookie.serialize('gitlabToken', '', {
path: '/',
expires: new Date('Thu, 01 Jan 1970 00:00:01 GMT')
})
);
} finally {
return response;
}
}
);
export const getSession: GetSession = function ({ locals }) {
2022-02-10 14:47:44 +00:00
return {
version,
...locals.session.data
2022-02-10 14:47:44 +00:00
};
};
export async function handleError({ error, event }) {
if (!dev) sentry.captureException(error, event);
2022-02-10 14:47:44 +00:00
}