0

I am currently working on a nuxtJS app in which the session seems to be lost after any refresh (although only in dev, not while deployed). I've tried to look up in the auth module of nuxt, and have tried many answers on google, but nothing seems to work and I'm a bit lost.

nuxt.config.js

auth: {
    strategies: {
      local: {
        scheme: 'refresh',
        token: {
          property: 'token',
          maxAge: 3600,
          global: true,
        },
        refreshToken: {
          property: 'refresh_token',
          data: 'refresh_token',
          maxAge: 60 * 60 * 24 * 30,
        },
        user: {
          property: 'user',
        },
        endpoints: {
          login: { url: '/authentication_token', method: 'post' },
          refresh: { url: '/refresh/token', method: 'post' },
          logout: false,
          user: { url: '/api/user', method: 'get' },
        },
        autoLogout: true,
      },
    },
  },

LoginMenu.js

 methods: {
   async onSubmit() {
     try {
       const response = await this.$auth.loginWith('local', {
         data: this.login,
       });
       if (response.status === 200) {
         await this.$auth.setUser({
           email: this.login.email,
           password: this.login.password,
         });
         await this.$router.push('/');
       }
       else {
         this.loginFail();
       }
     }
     catch (e) {
       this.loginFail();
     }
   },
   loginFail() {
     this.showError = true;
   },
 },

nuxt auth.js

import Middleware from './middleware'
import { Auth, authMiddleware, ExpiredAuthSessionError } from '~auth/runtime'

// Active schemes
import { RefreshScheme } from '~auth/runtime'

Middleware.auth = authMiddleware

export default function (ctx, inject) {
 // Options
 const options = {
 "resetOnError": false,
 "ignoreExceptions": false,
 "scopeKey": "scope",
 "rewriteRedirects": true,
 "fullPathRedirect": false,
 "watchLoggedIn": true,
 "redirect": {
   "login": "/login",
   "logout": "/",
   "home": "/",
   "callback": "/login"
 },
 "vuex": {
   "namespace": "auth"
 },
 "cookie": {
   "prefix": "auth.",
   "options": {
     "path": "/"
   }
 },
 "localStorage": {
   "prefix": "auth."
 },
 "defaultStrategy": "local"
}

 // Create a new Auth instance
 const $auth = new Auth(ctx, options)

 // Register strategies
 // local
 $auth.registerStrategy('local', new RefreshScheme($auth, {
 "token": {
   "property": "token",
   "maxAge": 3600,
   "global": true
 },
 "refreshToken": {
   "property": "refresh_token",
   "data": "refresh_token",
   "maxAge": 2592000
 },
 "user": {
   "property": "user"
 },
 "endpoints": {
   "login": {
     "url": "/authentication_token",
     "method": "post"
   },
   "refresh": {
     "url": "/refresh/token",
     "method": "post"
   },
   "logout": false,
   "user": {
     "url": "/api/user",
     "method": "get"
   }
 },
 "autoLogout": true,
 "name": "local"
}))

 // Inject it to nuxt context as $auth
 inject('auth', $auth)
 ctx.$auth = $auth

 // Initialize auth
 return $auth.init().catch(error => {
   if (process.client) {
     // Don't console log expired auth session errors. This error is common, and expected to happen.
     // The error happens whenever the user does an ssr request (reload/initial navigation) with an expired refresh
     // token. We don't want to log this as an error.
     if (error instanceof ExpiredAuthSessionError) {
       return
     }

     console.error('[ERROR] [AUTH]', error)
   }
 })
}

Kalnode
  • 9,386
  • 3
  • 34
  • 62
Simon
  • 619
  • 2
  • 9
  • 23
  • Does this answer your question? [Nuxt.js vuex store not persisted](https://stackoverflow.com/questions/66868348/nuxt-js-vuex-store-not-persisted) – kissu Dec 13 '21 at 11:15
  • i don't think so, cause the rest of my store is persisted – Simon Dec 14 '21 at 09:17

0 Answers0