added userId to locals
This commit is contained in:
parent
ae4400b02e
commit
dde23adef5
4 changed files with 12 additions and 3 deletions
|
@ -2,6 +2,7 @@ import type { Handle } from '@sveltejs/kit';
|
|||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
event.locals.token = await event.cookies.get('token');
|
||||
event.locals.userId = await event.cookies.get('UID');
|
||||
|
||||
return await resolve(event);
|
||||
};
|
|
@ -9,7 +9,7 @@ export async function load({locals}) {
|
|||
}
|
||||
|
||||
export const actions: Actions = {
|
||||
login: async ({request, fetch, cookies}) => {
|
||||
login: async ({request, fetch, cookies, locals}) => {
|
||||
const formData = await request.formData();
|
||||
|
||||
const response = await fetch('/api/auth/login', {
|
||||
|
@ -27,7 +27,14 @@ export const actions: Actions = {
|
|||
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");
|
||||
} else {
|
||||
|
|
|
@ -30,7 +30,7 @@ export async function POST({request}) {
|
|||
// @ts-ignore
|
||||
const token = jwt.sign(user, process.env.JWT_SECRET, { expiresIn: "1h" });
|
||||
logger.debug(`Generated a JWT token for user ${user.email}.`)
|
||||
return json({token: token});
|
||||
return json({token: token, userId: user.id});
|
||||
|
||||
} else {
|
||||
return error(400, {message: "Email ou mot de passe invalide."});
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
export async function load({ fetch }) {
|
||||
|
||||
try {
|
||||
// Appel API ou récupération de données
|
||||
const res = await fetch('/api/channels', {
|
||||
|
|
Loading…
Add table
Reference in a new issue