Build a Twitter Clone With Adonis and Vue

Securing Routes With Middleware

Adonis Auth package comes with a auth middleware which automates the flow of authenticating specific routes by adding the middleware on them. The auth middleware has been registered for us because we chose the api-only blueprint while creating our app. So we can just start using it. We'll add the auth middleware to some routes we want secured. That is, routes only authenticated users will be able access.

So to secure a route, we simply add the auth middleware to the route as below:

// this is just a sample for demonstration, it is not part of the app code

Route
  .get('sample', 'SampleController.profile')
  .middleware(['auth:jwt'])

Because we are authenticating with JWT, we also have to indicate that in the auth middleware auth:jwt.