diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 9b71e0b..61c1489 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -4,16 +4,20 @@ export async function load({ locals, url, fetch }) { const token = locals.token; if (token == undefined && url.pathname !== "/") { - redirect(301, "/"); + redirect(301, '/'); } - const res = await fetch(`/api/users/${locals.userId}`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json' - } - }); - const user = await res.json(); + let user = null; + if (locals.userId !== undefined) { + const res = await fetch(`/api/users/${locals.userId}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }); + + user = await res.json(); + } return { token, user } diff --git a/src/routes/api/auth/register/+server.ts b/src/routes/api/auth/register/+server.ts index 674d547..dfbb955 100644 --- a/src/routes/api/auth/register/+server.ts +++ b/src/routes/api/auth/register/+server.ts @@ -44,7 +44,7 @@ export async function POST({request}) { // @ts-ignore const token = jwt.sign(newUser, process.env.JWT_SECRET, { expiresIn: "1h" }); logger.debug(`Generated a JWT token for user ${newUser.email}.`) - return json({token: token}); + return json({token: token, userId: newUser.id}); } catch (e) { logger.error(e);