fixed problem with id not saving in cookies when creating an account

This commit is contained in:
Nabil Ould Hamou 2024-12-09 16:08:15 +01:00
parent 12835a26c6
commit e09f30bff3
2 changed files with 13 additions and 9 deletions

View file

@ -4,16 +4,20 @@ export async function load({ locals, url, fetch }) {
const token = locals.token; const token = locals.token;
if (token == undefined && url.pathname !== "/") { if (token == undefined && url.pathname !== "/") {
redirect(301, "/"); redirect(301, '/');
} }
const res = await fetch(`/api/users/${locals.userId}`, { let user = null;
method: 'GET', if (locals.userId !== undefined) {
headers: { const res = await fetch(`/api/users/${locals.userId}`, {
'Content-Type': 'application/json' method: 'GET',
} headers: {
}); 'Content-Type': 'application/json'
const user = await res.json(); }
});
user = await res.json();
}
return { token, user } return { token, user }

View file

@ -44,7 +44,7 @@ export async function POST({request}) {
// @ts-ignore // @ts-ignore
const token = jwt.sign(newUser, process.env.JWT_SECRET, { expiresIn: "1h" }); const token = jwt.sign(newUser, process.env.JWT_SECRET, { expiresIn: "1h" });
logger.debug(`Generated a JWT token for user ${newUser.email}.`) logger.debug(`Generated a JWT token for user ${newUser.email}.`)
return json({token: token}); return json({token: token, userId: newUser.id});
} catch (e) { } catch (e) {
logger.error(e); logger.error(e);