SyQual 10 S.A.S
SyQual 10 S.A.S

Reputation: 75

Capture error 401 adonis.js error E_INVALID_SESSION: Invalid session

I have a login developed in adonis and the routes already have the .middleware ('auth') to guarantee that they are only used when the user has made the entry, but when he has not done it the error 401 E_INVALID_SESSION: Invalid session, I want address to login and not that error

Upvotes: 2

Views: 1269

Answers (1)

crbast
crbast

Reputation: 2302

I advise you to create your own middleware:

like this:

'use strict'
/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */

class AuthVerif {
  /**
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Function} next
   */
  async handle({ response, auth }, next) {
    // call next to advance the request
    try {
      await auth.check()
    } catch (error) {
      // custom error response
      return response.status(401).send({ message: error.message })
    }
    await next()
  }
}

module.exports = AuthVerif

Upvotes: 1

Related Questions