1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-24 07:19:36 +02:00
AdventureLog/frontend/src/routes/+page.server.ts
2024-09-06 23:35:48 -04:00

53 lines
1.2 KiB
TypeScript

const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
import { redirect, type Actions } from '@sveltejs/kit';
const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
export const actions: Actions = {
setTheme: async ({ url, cookies }) => {
const theme = url.searchParams.get('theme');
// change the theme only if it is one of the allowed themes
if (
theme &&
[
'light',
'dark',
'night',
'retro',
'forest',
'aqua',
'forest',
'aestheticLight',
'aestheticDark',
'emerald'
].includes(theme)
) {
cookies.set('colortheme', theme, {
path: '/',
maxAge: 60 * 60 * 24 * 365
});
}
},
logout: async ({ cookies }: { cookies: any }) => {
const cookie = cookies.get('auth') || null;
if (!cookie) {
return;
}
const res = await fetch(`${serverEndpoint}/auth/logout/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Cookie: cookies.get('auth')
}
});
if (res.ok) {
cookies.delete('auth', { path: '/', secure: false });
cookies.delete('refresh', { path: '/', secure: false });
return redirect(302, '/login');
} else {
return redirect(302, '/');
}
}
};