Creating Middleware
Learn how to create middleware in AdonisJs
We'll cover the following...
We'll cover the following...
Middleware is created inside the folder app/Middleware and registered inside the start/kernel.js file.
Creating named middleware
Let’s say we want to create authentication middleware to only let users who are logged in access our website. We will use named middleware to achieve this.
'use strict'; class AuthRequired { async handle ({ auth, response }, next) { try { await auth.check() } catch (error) { return response.json({ error: "Unauthorized access" }) } // call next() to advance the request await next() } } module.exports = AuthRequired;
Press Run ...