Why my session is invalid?

I have this method “login”, the method login the user its simple to understand.
when i login it works and it printed to me the user and response.

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

      const { email, password } = request.all();
      
      try{
        const isLogeddin = await auth.check()
        const user = await auth.getUser()
        
        if(isLogeddin){
          
          response.status(401).send({ alert: 'User already logged in!',  user})
          
        
        }
      }catch(err){
        try{
          await auth.attempt(email, password)
          const user = await auth.getUser()
          response.status(200).send({ message : 'Sucessfully Logged in!', user })
        }catch(err){
          response.status(401).send({ error: 'Credentials Invalid!' })
        }
      }
      
    }

But, when i access logout method, i get:

E_INVALID_SESSION: Invalid session

My logout method for test.

async logout({ request, response, auth}) {
      
      const test = await auth.check()

      
    }
async show({request, response, auth}){

   const user = await auth.getUser()

   return user

  }

Whats going on if i can get auth.getUser() in login but i cant get it in logout?

The debug output always give to me something like that when access logout method:

adonis:middlewarebase executing NaN server middleware +0ms
adonis:middlewarebase executing 6 global and named middleware +13ms

start/app.js

'use strict'

const providers = [
  '@adonisjs/framework/providers/AppProvider',
  '@adonisjs/session/providers/SessionProvider',
  '@adonisjs/auth/providers/AuthProvider',
  '@adonisjs/bodyparser/providers/BodyParserProvider',
  '@adonisjs/cors/providers/CorsProvider',
  '@adonisjs/lucid/providers/LucidProvider',
  '@adonisjs/validator/providers/ValidatorProvider',
  'adonis-acl/providers/AclProvider',
  '@adonisjs/mail/providers/MailProvider',
  'adonis-bumblebee/providers/BumblebeeProvider',
]

const aceProviders = [
  '@adonisjs/lucid/providers/MigrationsProvider',
  'adonis-acl/providers/CommandsProvider',
  'adonis-bumblebee/providers/CommandsProvider',

]

const aliases = {
  Role: 'Adonis/Acl/Role',
  Permission: 'Adonis/Acl/Permission',
}

const commands = []

module.exports = { providers, aceProviders, aliases, commands }

start/kernel.js

'use strict'

/** @type {import('@adonisjs/framework/src/Server')} */

const Server = use('Server')

const globalMiddleware = [

'Adonis/Middleware/Session',

'Adonis/Middleware/AuthInit',

'Adonis/Middleware/BodyParser',

'App/Middleware/ConvertEmptyStringsToNull',

'Adonis/Acl/Init',

]


const namedMiddleware = {

auth: 'Adonis/Middleware/Auth',

guest: 'Adonis/Middleware/AllowGuestOnly',

is: 'Adonis/Acl/Is',

can: 'Adonis/Acl/Can',

}


const serverMiddleware = [

// 'Adonis/Middleware/Static',

'Adonis/Middleware/Cors'

]

Server

.registerGlobal(globalMiddleware)

.registerNamed(namedMiddleware)

.use(serverMiddleware)

start/routes.js

'use strict'

const Route = use('Route')


Route.post('/user','UserController.show').middleware('auth')

Route.post('/logout', 'SessionController.logout').middleware('auth')

Route.post('/login', 'SessionController.login')

When I log in by route /login, it works normally, and when I’m logged in, I access /user, it gives me back the user as expected in controller.

But when I access the logout method it gives me an invalid session error, I can’t understand

Hi!

Are you sure session cookie is attached to logout request?

2 Likes