AdonisJS provides a standard way to define a middleware and enable access to routes based on defined validation rules. This middleware is leveraged to allow role-based access to routes.
Your start
| routes.js
file will have something akin to below -
|
|
First, we will group routes for the different roles and introduce the middleware.
|
|
register
andlogin
methods are available to unauthenticated and authenticated users- To do
create
andindex
are available only to authenticated users.auth
is provided by AdonisJS - Deleting to do is allowed only for admin
Now, we write the actual middleware for admin
.
Create a admin.js
file in app
| Middleware
folder under root. Introduce the following code -
|
|
What happens when a user tries to access the delete
route -
- Adonis executes the
auth
andadmin
middleware auth
passes ok if user is authenticatedadmin
checks if theuser.role_cd
, a custom field, has the value ‘admin’. If this does not check out, an exception is thrownInvalidAccessException
is a custom exception that returns a404
with a custom message (see [custom exceptions in Adonis)(/custom-exceptions-in-adonisjs/)
In a previous post we had a short discussion on implementing access rules in Adonis based on roles. Should you implement that vs. implementing the above in routes?
Well - it depends.
- If the entire route is cordoned off to roles, you have a cleaner approach using role-based validation at the route level.
- For everything else, let your controller take the lead for role-based authentication
In the real world, you would want to implement a separate service for role-based auth. This service can be called by your route middleware, or your controllers depending on your use case.