2024-04-03 22:59:05 +00:00
|
|
|
import { lucia } from "$lib/server/auth";
|
|
|
|
import { fail, redirect } from "@sveltejs/kit";
|
|
|
|
|
|
|
|
import type { Actions, PageServerLoad } from "./$types";
|
|
|
|
|
2024-04-17 00:01:19 +00:00
|
|
|
export const load: PageServerLoad = async (event: { locals: { user: any; }; }) => {
|
2024-04-03 22:59:05 +00:00
|
|
|
if (event.locals.user)
|
|
|
|
return {
|
|
|
|
user: event.locals.user,
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
user: null,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-06 12:27:42 +00:00
|
|
|
// handle the logout action
|
2024-04-03 22:59:05 +00:00
|
|
|
export const actions: Actions = {
|
2024-04-17 00:01:19 +00:00
|
|
|
logout: async (event) => {
|
2024-04-03 22:59:05 +00:00
|
|
|
if (!event.locals.session) {
|
|
|
|
return fail(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
await lucia.invalidateSession(event.locals.session.id);
|
|
|
|
const sessionCookie = lucia.createBlankSessionCookie();
|
|
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
|
|
path: ".",
|
|
|
|
...sessionCookie.attributes,
|
|
|
|
});
|
|
|
|
return redirect(302, "/login");
|
|
|
|
},
|
2024-04-17 00:01:19 +00:00
|
|
|
setTheme: async ( { url, cookies }) => {
|
|
|
|
const theme = url.searchParams.get("theme");
|
|
|
|
if (theme) {
|
|
|
|
cookies.set("colortheme", theme, {
|
|
|
|
path: "/",
|
|
|
|
maxAge: 60 * 60 * 24 * 365,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-04-03 22:59:05 +00:00
|
|
|
};
|