Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

How to implement route guards with redirects with sveltekit's adapter-static?

$
0
0

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?


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>