Add user login logout

This commit is contained in:
Esa Kataja
2024-11-19 22:08:38 +02:00
parent c22f44b483
commit 38afd41ca0
15 changed files with 218 additions and 12 deletions
+28
View File
@@ -0,0 +1,28 @@
import { redirect, type RequestEvent } from '@sveltejs/kit';
export const authenticateUser = (event: RequestEvent) => {
const {cookies} = event;
const userCookie = cookies.get('user');
if (!userCookie) {
return null
}
const decodedCookie = decodeURIComponent(userCookie);
const parsedCookie = JSON.parse(decodedCookie);
if (!parsedCookie) {
return null
}
return parsedCookie
}
export const logoutUser = (event: RequestEvent) => {
const {cookies} = event;
cookies.delete('user', {
path: '/',
sameSite: 'strict',
secure: false,
});
throw redirect(302, '/login');
}