TanStackT
TanStack2y ago
40 replies
uniform-turquoise

Context & protected routes

Hi everyone, i'm having some issues with protected routes:

If I go to a route from a link, the context is still passed, and there is not issues.

But if I go to the URL from my browser like localhost/account then the context is null for like 0.2s.

But using this condition in my Route:
    beforeLoad: ({ context, location }) => {
        if (!context.auth.isAuthenticated) {

            console.log("redirecting to /")
            console.log(context.auth)

            throw redirect({
                to: '/',
                search: {
                    redirect: location.href,
                },
            })
        }
    },


It redirect the user to the main page.

Any ways to avoid that?
Was this page helpful?