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';
|
2022-02-18 12:59:23 +00:00
|
|
|
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',
|
2022-02-21 11:35:20 +00:00
|
|
|
expires: event.locals.session.data.expires
|
2022-02-18 20:37:40 +00:00
|
|
|
};
|
2022-02-18 12:59:23 +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-18 12:59:23 +00:00
|
|
|
}
|
2022-02-10 14:47:44 +00:00
|
|
|
}
|
2022-02-18 12:59:23 +00:00
|
|
|
|
2022-02-10 14:47:44 +00:00
|
|
|
response = await resolve(event, {
|
|
|
|
ssr: !event.url.pathname.startsWith('/webhooks/success')
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2022-02-18 12:59:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-02-18 12:59:23 +00:00
|
|
|
export const getSession: GetSession = function ({ locals }) {
|
2022-02-10 14:47:44 +00:00
|
|
|
return {
|
|
|
|
version,
|
2022-02-18 12:59:23 +00:00
|
|
|
...locals.session.data
|
2022-02-10 14:47:44 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function handleError({ error, event }) {
|
2022-02-18 12:59:23 +00:00
|
|
|
if (!dev) sentry.captureException(error, event);
|
2022-02-10 14:47:44 +00:00
|
|
|
}
|