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

Issues on nuxt-auth with SPA & Laravel Sanctum cookie based auth context

Open Scoottykun opened this issue 3 years ago • 0 comments

Hello everyone !

I'm seeking for some help about an issue i'm encountering on my freshly created project based on these alternatives modules.

I successfully managed to get a working authentification based on the laravel-sanctum provider in my SPA Nuxt 3 app, however when i'm logged in and on the dashboard of my app for example (with all user informations within the pinia auth module), if i reload my page i'm directly redirected to the login page and i loose my previous authentication. In the console i don't see any XXR request that should normally fetch user informations (/api/v1/user/me) and my Pinia Auth store module has an "user: false" state (which is kind of normal since the related request is not triggered). Also after this login redirection an unknow (to me) error appears in the console, which i'm not able to understand and seems to break the login page display TypeError: Cannot read properties of null (reading 'parentNode')

image

Here is my nuxt.config.ts :

import { defineNuxtConfig } from 'nuxt'
import eslintPlugin from 'vite-plugin-eslint'
import svgLoader from "vite-svg-loader"

// @ts-ignore
export default defineNuxtConfig({
    ssr: false,
    css: [
        'vuetify/lib/styles/main.sass',
        '@mdi/font/css/materialdesignicons.min.css'
    ],
    axios: {
        baseUrl: `${process.env.BACKEND_URL}${process.env.API_PATH}`,
        proxy: process.env.ENV === 'development',
        credentials: true
    },
    proxy: {
        '/laravel': {
            target: process.env.BACKEND_URL,
            rewrite: (path) => path.replace(/^\/laravel/, ''),
        },
        '/sanctum': {
            target: process.env.BACKEND_URL
        }
    },
    router: {
        middleware: ['auth']
    },
    auth: {
        globalMiddleware: true,
        // @ts-ignore
        redirect: {
            login: '/login',
            logout: '/login',
            callback: '/login',
            home: '/'
        },
        strategies: {
            // @ts-ignore
            laravelSanctum: {
                provider: 'laravel/sanctum',
                cookie: {
                    server: true,
                    name: 'XSRF-TOKEN'
                },
                endpoints: {
                    csrf: { url: '/sanctum/csrf-cookie' },
                    login: { url: '/laravel/api/v1/user/login' },
                    logout: { url: '/laravel/api/v1/user/logout' },
                    user: { url: '/laravel/api/v1/user/me' }
                },
                user: {
                    property: {
                        client: false,
                        server: false
                    },
                    autoFetch: true
                }
            }
        }
    },
    build: {
        transpile: ['vuetify'],
    },
    eslint: {
        fix: false
    },
    vite: {
        plugins: [
            // eslintPlugin(),
            svgLoader()
        ],
        define: {
            'process.env.DEBUG': false
        }
    },
    modules: [
        '@nuxtjs-alt/auth',
        '@nuxtjs-alt/axios',
        '@nuxtjs-alt/proxy',
        '@pinia/nuxt',
    ],
    publicRuntimeConfig: {
        BACKEND_URL: process.env.BACKEND_URL,
        API_PATH: process.env.API_PATH,
    },
})

Would gladly receive any information that could help me understand why the login redirection is triggered on page reload and why the user information request is not automatically fetched.

Thanks !

Scoottykun avatar Jun 16 '22 16:06 Scoottykun