Redirect if not logged in triggered too late?
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
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?