Getting Started

Installation

Install and configure @eienjs/adonisjs-simple-auth in your AdonisJS v7 project.

You can install and configure @eienjs/adonisjs-simple-auth with a single Ace command, or set it up manually.

Quick Installation

Run the ace add command to install the package and automatically execute its configuration codemods:

node ace add @eienjs/adonisjs-simple-auth
The node ace add command automatically registers the provider, commands, and middleware, and publishes the configuration and middleware files.

Manual Installation

If you prefer to install the package first and run the configuration step separately:

Install the package from npm

pnpm add @eienjs/adonisjs-simple-auth

Run the configure command

Execute the AdonisJS configure command to publish the required files and update your project setup:

node ace configure @eienjs/adonisjs-simple-auth

What the Configure Command Does

Running node ace add or node ace configure performs the following changes in your project:

  1. Registers Commands & Provider in adonisrc.ts:
    adonisrc.ts
    import { defineConfig } from '@adonisjs/core/app';
    
    export default defineConfig({
      commands: [
        // ...other commands
        () => import('@eienjs/adonisjs-simple-auth/commands'),
      ],
      providers: [
        // ...other providers
        () => import('@eienjs/adonisjs-simple-auth/providers/simple_auth_provider'),
      ],
    });
    
  2. Publishes the Configuration File at config/simpleauth.ts:
    config/simpleauth.ts
    import { defineConfig } from '@eienjs/adonisjs-simple-auth';
    import env from '#start/env';
    
    const simpleAuthConfig = defineConfig({
      apiKeyHeader: 'x-api-key',
      apiKeyValue: env.get('API_KEY'),
    });
    
    export default simpleAuthConfig;
    
  3. Publishes the Route Middleware at app/middleware/api_key_middleware.ts:
    app/middleware/api_key_middleware.ts
    import type { HttpContext } from '@adonisjs/core/http';
    import type { NextFn } from '@adonisjs/core/types/http';
    
    export default class ApiKeyMiddleware {
      async handle(ctx: HttpContext, next: NextFn) {
        ctx.simpleAuth.authenticate();
    
        return next();
      }
    }
    
  4. Registers Router and Named Middleware in start/kernel.ts:
    start/kernel.ts
    import router from '@adonisjs/core/services/router';
    
    router.use([
      // ...other router middleware
      () => import('@eienjs/adonisjs-simple-auth/middleware/simple_auth_middleware'),
    ]);
    
    export const middleware = router.named({
      // ...other named middleware
      apiKey: () => import('#middleware/api_key_middleware'),
    });
    

Environment Validation & Verification

Add API_KEY to your environment validation

Open start/env.ts and add validation rules for the API_KEY environment variable:

start/env.ts
import { Env } from '@adonisjs/core/env';

export default await Env.create(new URL('../', import.meta.url), {
  // ...other variables
  API_KEY: Env.schema.secret(),
});

Generate your API key

Run the built-in Ace command to generate a secure token and write API_KEY to your .env file:

node ace generate:api-key

Verify the setup

Protect a test route in start/routes.ts using middleware.apiKey() and send a request with the x-api-key header. See Protecting Routes for complete examples.

Learn how to customize the header name and secret value in the Configuration guide.