NuxtN
Nuxt13mo ago
4 replies
Neamix

Middleware doesn't print log

Here's a grammatically corrected version of your text:

Hello everyone, I am encountering a strange error in my middleware I created a middleware that redirects the user to the registration page if the authentication token does not exist or is invalid everything works fine on the Windows environment, and the server (Ubuntu environment) seems fine when I first log in allowing me to access the dashboard without any issue. However, when I refresh the page, I get redirected to the registration page, even though the token exists and is valid and After further investigation I found that the issue is in the last line of the code. I tried to print the error, but nothing is printed, even after I disabled the redirect. Interestingly, after disabling the redirect, the dashboard was able to use the token to identify the user.

import { useCookie } from "nuxt/app";
import { useAuthenticationStore } from "../stores/Authentication/authentication";
import { useRouter } from "vue-router";

export default defineNuxtRouteMiddleware(async (to, from) => {
    let layout = to.meta.layout;
    const route = useRouter()

    // In case the layout not dashboard then this not in consirn 
    if (layout != 'dashboard') return true;

    // Check if this is the dashboard but the user doesnt have token
    if (layout == 'dashboard' && !useCookie('__Secure_TK').value?.length) navigateTo('/register');

    // Get user data from the token
    let authenticationStore = useAuthenticationStore();    

    try {
        let {data} = await authenticationStore.me();
        if (data.is_active) authenticationStore.signInUser(data)
        else return navigateTo('/register');
    } catch ($error) {
        console.log($error);
        // return navigateTo('/register') -> problem line
    }

   
})
Was this page helpful?