How to use auth middleware with different authentication scheme for different routes?


#1

For Example:
One Middleware for “auth:session” & another for “auth:jwt”
This is what currently I am using

const namedMiddleware = {
  auth: 'Adonis/Middleware/Auth'
}

Can I have something like this

const namedMiddleware = {
  'auth:session': 'Adonis/Middleware/Auth',
'auth:jwt':'Adonis/Middleware/Api'
}

#2

Hey @nir-jas! :wave:

Why would you want to do that?
Adonis is already providing an auth middleware that takes as argument differents schemes.


#3

Hi Romain, Thanks for your prompt reply.

I have used two Authentication in my APP.

Session for App Views and JWT for API. I want to handle redirect of routes in Middleware and want to validate API request as well in middleware.


#4

Simply use the middleware auth:session and auth:jwt.


#5

How to use auth middleware with different authentication scheme for different routes?


#6

Ya, I am using it but I want to extend middleware based on authentication scheme


#7

As said, the auth middleware take as props different schemes.

https://adonisjs.com/docs/4.1/middleware#_middleware_props


#8

Props will be available as the third argument in middleware’s handle method. This is what I was looking for. Thanks @romain.lanz