From b36ec3ef0837af50742e348846698783a8391fa4 Mon Sep 17 00:00:00 2001 From: Frank Noirot Date: Sat, 20 Jul 2024 07:39:44 -0400 Subject: [PATCH] Replace hooks.server.ts with +layout.server.ts (#146) --- src/hooks.server.ts | 63 -------------------- src/routes/(sidebarLayout)/+layout.server.ts | 61 +++++++++++++++++-- 2 files changed, 56 insertions(+), 68 deletions(-) delete mode 100644 src/hooks.server.ts diff --git a/src/hooks.server.ts b/src/hooks.server.ts deleted file mode 100644 index 4f15ae4..0000000 --- a/src/hooks.server.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { redirect } from '@sveltejs/kit' -import { AUTH_COOKIE_NAME } from '$lib/cookies' -import { SIGN_OUT_PARAM } from '$lib/paths' -import { DOMAIN, PLAYWRIGHT_MOCKING_HEADER } from '$lib/consts' -import { hooksUserMocks, isUserMock } from '$lib/mocks' - -export const handle = async ({ event, resolve }) => { - const mock = event.request.headers.get(PLAYWRIGHT_MOCKING_HEADER) - const token = import.meta.env.PROD - ? event.cookies.get(AUTH_COOKIE_NAME) - : import.meta.env.VITE_TOKEN - - if (!token) { - return resolve(event) - } - - const currentUser = await event - .fetch(import.meta.env.VITE_API_BASE_URL + '/user', { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}` - } - }) - .then((res) => res.json()) - .catch((e) => { - // If the user had a token but there was an error fetching the user, - //delete the token, because it was likely revoked or expired - console.error('Error fetching user:', e) - signOut() - }) - - if (!currentUser) { - event.locals.user = undefined - } else { - if ('error_code' in currentUser) { - console.error('Error fetching user:', currentUser.error_code) - signOut() - } - - event.locals.user = currentUser - if (mock !== null) { - const userMock = isUserMock(mock) - event.locals.user = userMock ? hooksUserMocks[userMock](currentUser) : currentUser - } - } - - const query = event.url.searchParams.get(SIGN_OUT_PARAM) - - if (Boolean(query) == true) { - signOut() - } - return resolve(event) - - /** - * Shared sign out function - */ - function signOut() { - event.cookies.delete(AUTH_COOKIE_NAME, { domain: DOMAIN, path: '/' }) - event.url.searchParams.delete(SIGN_OUT_PARAM) - throw redirect(303, '/') - } -} diff --git a/src/routes/(sidebarLayout)/+layout.server.ts b/src/routes/(sidebarLayout)/+layout.server.ts index 6e7fdad..5b21c51 100644 --- a/src/routes/(sidebarLayout)/+layout.server.ts +++ b/src/routes/(sidebarLayout)/+layout.server.ts @@ -1,11 +1,62 @@ -import { DOMAIN } from '$lib/consts.js' +import { DOMAIN, PLAYWRIGHT_MOCKING_HEADER } from '$lib/consts.js' import { AUTH_COOKIE_NAME } from '$lib/cookies.js' +import { hooksUserMocks, isUserMock } from '$lib/mocks.js' +import { SIGN_OUT_PARAM } from '$lib/paths.js' import { redirect } from '@sveltejs/kit' -export const load = async ({ locals, cookies }) => { - // redirect user if not logged in - if (!locals.user) { +export const load = async ({ cookies, request, url, fetch }) => { + if (url.searchParams.get(SIGN_OUT_PARAM)) { + signOut() + } + + const mockRequestHeader = request.headers.get(PLAYWRIGHT_MOCKING_HEADER) + const token = import.meta.env.PROD ? cookies.get(AUTH_COOKIE_NAME) : import.meta.env.VITE_TOKEN + + if (!token) { + signOut() + } + + const currentUser = await fetch(import.meta.env.VITE_API_BASE_URL + '/user', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } + }) + .then((res) => res.json()) + .catch((e) => { + // If the user had a token but there was an error fetching the user, + //delete the token, because it was likely revoked or expired + console.error('Error fetching user:', e) + signOut() + }) + + if (!currentUser) { + signOut() + } else { + if ('error_code' in currentUser) { + console.error('Error fetching user:', currentUser.error_code) + signOut() + } else if (mockRequestHeader !== null) { + const userMock = isUserMock(mockRequestHeader) + return { + user: userMock ? hooksUserMocks[userMock](currentUser) : currentUser, + token + } + } + + // Return the user and token + return { + user: currentUser, + token: token + } + } + + /** + * Shared sign out function + */ + function signOut() { cookies.delete(AUTH_COOKIE_NAME, { domain: DOMAIN, path: '/' }) - throw redirect(302, '/') + throw redirect(303, '/') } }