NuxtN
Nuxt13mo ago
4 replies
Neamix

Middleware cant access useCookie on server

Good day everyone, i am trying to use useCookie to store my passport access token then i added middleware to check if the token exist go a head and identify the user if not redirect him to register in it on local windows enviroment every thing work great but when i deployed on the server it always redirect me to register page even my token exist here is my code snipt
import { useCookie } from "nuxt/app";
import { useAuthenticationStore } from "../stores/Authentication/authentication";

export default defineNuxtRouteMiddleware(async (to, from) => {
    let layout = to.meta.layout;
    let isAuthLayout = layout == 'dashboard' || layout == 'messenger';
    
    // In case the layout not dashboard then this not in consirn 
    if (!isAuthLayout) return true;

    // Check if this is the dashboard but the user doesnt have token
    if (!useCookie('__Secure_TK').value)  return 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,$error.status)
        // return navigateTo('/register');
    }

   
})
Was this page helpful?