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.

1 Like

Hi :wave:,

You can use middleware and Auth object.

Example :

async handle({ response, auth }, next) {
    // call next to advance the request
    try {
      await auth.check()
    } catch (error) {
      return response.route('login')
    }
    await next()
  }

Auth documentation

More examples

Have a nice day!

2 Likes

Thank you CrBast :slight_smile:

1 Like