Nuxt - Session file doesnt persist after refresh


#1

Hi.
I’m trying to implement a Nuxt Vuex Store Authentication with Adonis Auth using Sessions.

I’m using NuxtServerInit to check if any user is logged in but it didn’t work as auth.check() and auth.getUser() always send false although I did a login.

So I found that the problem is on Sessions. It doesn’t persist after a refresh.

The sessions files are stored on tmp/sessions. It seems it creates a new file everytime I refresh the page.

This is the code I used to test.

UserController.js

async login({request, auth, response,session}){

    session.put('user','session user')

 }
  
async getLogin({auth, response,session}){

    console.log(session.get('user'));

}

/store/.index.js

async nuxtServerInit ({ dispatch }, { app }) {
        
       try{
          await dispatch('getCurrentUser')
        }catch(e){
          console.log(e);
        }
      },

      async getCurrentUser({commit}){

        const user = await this.$axios.$get('api.v1/login')

        console.log(user); // this returns null

      }

Also if I use Postman to send a get request a new session file is created in each request.

This is my config/session.js

module.exports = {
driver: Env.get(‘SESSION_DRIVER’, ‘file’),

cookieName: ‘adonis-session’,

clearWithBrowser: false,

age: ‘2h’,

cookie: {
httpOnly: true,
sameSite: true,
path: ‘/’
},

file: {
location: ‘sessions’
},

redis: ‘self::redis.local’
}


#2

Hey @jorgeyoma! :wave:

Does the value in your cookie change at every refresh too ?


#3

Hi @romain.lanz
I’m using file in sessions. It crates a new session file in every refresh.

The problem is somewhere at the nuxt-create-app config. I created a new app with adonis and vuetify with (https://nuxtjs.org/guide/installation). You can reproduce it this way:

npx create-nuxt-app <project-name>

Then set cors to “*” and csrf to false and added this routes in start/routes,js

const Route = use('Route')

Route.post('session',({request,response,session})=>{

session.put('mydata',request.all())

response.send(session.get('mydata'))

})

Route.get('session',({response,session})=>{

response.send(session.get('mydata'))

})

Route.any('*', 'NuxtController.render')

Then, go to postman and saved some info in the session with post. When I retrieve it with get the response is empty.

I’ll post this issue in the nuxt forum also.


#4

It seems that I found the problem. Its on config/session.js:

The default configuration is this

cookie: {
    httpOnly: true,
    sameSite: true,
    path: '/'
  },

when you change it to:

cookie: {
    httpOnly: true,
    sameSite: false,
    path: '/'
  },

The session info persist.

Please check if anyone can check if this should be the solution