I’m new to adonis js and I’m trying to create a middleware to restrict my endpoints. For my authentication I use custom auth guard (with JWT) documentation.
text
my middleware code
export default class AdminMiddleware {
async handle(
ctx: HttpContext,
next: NextFn,
options: {
guards?: (keyof Authenticators)[]
} = {}
) {
const user = await ctx.auth.authenticateUsing(options.guards)
const isAdmin = Role.MANAGER === user.roleId
if (!isAdmin) {
throw new Error("You don't have permission to use this middleware")
}
const output = await next()
return output
}
}
my route
router
.group(() => {
router.post('/add-user', '#controllers/users_controller.addUser')
})
.prefix('api/v1/user')
.use(middleware.admin())
whether the user is the right role or not. it shows that I am not authorized.
result after send a post request
and I have the same result when I use the default middleware auth(). how can I solve this problem
New contributor
AMARY ANGE KEVIN MELESS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.