nuxt-module-alternatives icon indicating copy to clipboard operation
nuxt-module-alternatives copied to clipboard

Redirect if not logged in triggered too late?

Open ahoiroman opened this issue 3 years ago • 0 comments

Hello everybody,

My Sanctum Auth is working, but I mentioned something strange.

If I got a protected route - e.g. "/dashboard", which contains a component, which does something like this:

let id = $auth.user.id;
useFetch(`/api/user/${id}/teams`, {
        server: false,
        credentials: "include",
        headers,
        ...opts,
    });

I am getting, if I am hitting "/dashboard" manually, this message in the browser:

Redirecting to /auth/signin

Bildschirmfoto 2022-06-02 um 09 30 30@2x

The redirect is not happening, the message just stays the same.

The log tells me this:

[nuxt] [request error] Cannot read properties of null (reading 'id')

Which points to the api-call:

/api/${$auth.user.id}/teams

If I edit the dashboard.vue, and adding this to the component, which has the api-call in it's <script setup>:

<TeamCard v-if="$auth.user.isLoggedIn"/>

the redirect is processed as expected.

So it seems the middleware does not redirect the unauthenticated user, before the protected route ("/dashboard") and the related page (dashboard.vue) is loaded?

ahoiroman avatar Jun 02 '22 07:06 ahoiroman