2024-07-08 11:44:39 -04:00
|
|
|
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
|
|
|
import { redirect, type Actions } from '@sveltejs/kit';
|
2024-11-26 17:39:10 -05:00
|
|
|
import { themes } from '$lib';
|
2024-12-01 09:52:04 -05:00
|
|
|
import { fetchCSRFToken } from '$lib/index.server';
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
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
|
2024-11-26 17:39:10 -05:00
|
|
|
if (theme && themes.find((t) => t.name === theme)) {
|
2024-07-08 11:44:39 -04:00
|
|
|
cookies.set('colortheme', theme, {
|
|
|
|
path: '/',
|
2024-11-27 11:25:33 -05:00
|
|
|
maxAge: 60 * 60 * 24 * 365, // 1 year
|
|
|
|
sameSite: 'lax'
|
2024-07-08 11:44:39 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-12-01 09:52:04 -05:00
|
|
|
logout: async (event) => {
|
|
|
|
let sessionId = event.cookies.get('sessionid');
|
|
|
|
let csrfToken = await fetchCSRFToken();
|
2024-07-08 11:44:39 -04:00
|
|
|
|
2024-12-01 09:52:04 -05:00
|
|
|
if (!sessionId) {
|
2024-07-08 11:44:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-01 09:52:04 -05:00
|
|
|
const res = await fetch(`${serverEndpoint}/_allauth/browser/v1/auth/session`, {
|
|
|
|
method: 'DELETE',
|
2024-07-08 11:44:39 -04:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2024-12-01 09:52:04 -05:00
|
|
|
Cookie: `sessionid=${sessionId}; csrftoken=${csrfToken}`,
|
|
|
|
'X-CSRFToken': csrfToken
|
|
|
|
},
|
|
|
|
credentials: 'include'
|
2024-07-08 11:44:39 -04:00
|
|
|
});
|
2024-12-01 09:52:04 -05:00
|
|
|
if (res.status == 401) {
|
2024-07-08 11:44:39 -04:00
|
|
|
return redirect(302, '/login');
|
|
|
|
} else {
|
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
2024-10-28 13:56:57 -04:00
|
|
|
},
|
|
|
|
setLocale: async ({ url, cookies }) => {
|
|
|
|
const locale = url.searchParams.get('locale');
|
2024-11-27 11:25:33 -05:00
|
|
|
// change the locale only if it is one of the allowed locales
|
|
|
|
if (locale) {
|
2024-10-28 13:56:57 -04:00
|
|
|
cookies.set('locale', locale, {
|
|
|
|
path: '/',
|
2024-11-27 11:25:33 -05:00
|
|
|
maxAge: 60 * 60 * 24 * 365 // 1 year
|
2024-10-28 13:56:57 -04:00
|
|
|
});
|
|
|
|
}
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
};
|