added userId to locals

This commit is contained in:
Nabil Ould Hamou 2024-12-03 19:20:00 +01:00
parent ae4400b02e
commit dde23adef5
4 changed files with 12 additions and 3 deletions

View file

@ -2,6 +2,7 @@ import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => { export const handle: Handle = async ({ event, resolve }) => {
event.locals.token = await event.cookies.get('token'); event.locals.token = await event.cookies.get('token');
event.locals.userId = await event.cookies.get('UID');
return await resolve(event); return await resolve(event);
}; };

View file

@ -9,7 +9,7 @@ export async function load({locals}) {
} }
export const actions: Actions = { export const actions: Actions = {
login: async ({request, fetch, cookies}) => { login: async ({request, fetch, cookies, locals}) => {
const formData = await request.formData(); const formData = await request.formData();
const response = await fetch('/api/auth/login', { const response = await fetch('/api/auth/login', {
@ -27,7 +27,14 @@ export const actions: Actions = {
maxAge: (60 * 60) * 30, maxAge: (60 * 60) * 30,
}); });
logger.debug("Successfully created a cookie for the user and proceeded with the login.") cookies.set('UID', data.userId, {
path: '/',
httpOnly: true,
sameSite: 'strict',
maxAge: (60 * 60) * 30,
});
logger.debug("Successfully created a cookie for the user and proceeded with the login.");
return redirect(302, "/chats"); return redirect(302, "/chats");
} else { } else {

View file

@ -30,7 +30,7 @@ export async function POST({request}) {
// @ts-ignore // @ts-ignore
const token = jwt.sign(user, process.env.JWT_SECRET, { expiresIn: "1h" }); const token = jwt.sign(user, process.env.JWT_SECRET, { expiresIn: "1h" });
logger.debug(`Generated a JWT token for user ${user.email}.`) logger.debug(`Generated a JWT token for user ${user.email}.`)
return json({token: token}); return json({token: token, userId: user.id});
} else { } else {
return error(400, {message: "Email ou mot de passe invalide."}); return error(400, {message: "Email ou mot de passe invalide."});

View file

@ -1,4 +1,5 @@
export async function load({ fetch }) { export async function load({ fetch }) {
try { try {
// Appel API ou récupération de données // Appel API ou récupération de données
const res = await fetch('/api/channels', { const res = await fetch('/api/channels', {