Skip to content
Github

adonisjs-simple-auth

This package enables you to create a single authentication key that allows you to authenticate just by including the key. This simplicity also allows a user to make calls easily, with cURL, with interactive docs, or even in their browser.

Install and configure Adonisjs Api Query using the following command.

node ace add @eienjs/adonisjs-simple-auth

The package will automatically register its service provider.

In your routes, you can protect routes using the apiKey middleware.

import router from '@adonisjs/core/services/router';
import { middleware } from '#start/kernel';

const HealthChecksController = () => import('#controllers/health_checks_controller');

const api = (): void => {
  router
    .get('/', (ctx) => {
      ctx.response.jsendSuccess('Service is up and running');
    })
    .as('main');

  router.get('/health', [HealthChecksController]).use(middleware.apiKey()).as('health');
};

export default api;

The api key middleware will check if the request has a api_key or custom header key set. It will check if the key is valid. If it is, the request will be allowed to continue. If not, it will return a 401 status code.