I have a bit of a problem with my client only app. The app works as intended during dev but the built bundle does not work as expected.
Here is where I think my problem is coming from.
I have hooks.js
with the following code:
export const handle = async ({ event, resolve }) => { const authCookie = event.cookies.get('authCookie'); if (event.url.pathname.startsWith('/app')) { if (typeof authCookie === 'undefined') { return new Response(null, { status: 302, headers: { location: '/login' } }); } try { const userData = JSON.parse(decodeURIComponent(authCookie)); event.locals.user = userData; return await resolve(event, {}); } catch (err) { return new Response(null, { status: 302, headers: { location: '/login' } }); } } return await resolve(event, {});};
the authCookie
is set in the login
route once a user logs in.
The problem is that when I browse the built bundle, I can successfully login and the cookie seems to be set but when I refresh the page, I get redirected to the login page.
What could be causing this issue and how can make my auth guard work in the statically built bundle?