Installation
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
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
npm install @eienjs/adonisjs-simple-auth
yarn 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:
- Registers Commands & Provider in
adonisrc.ts:adonisrc.tsimport { 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'), ], }); - Publishes the Configuration File at
config/simpleauth.ts:config/simpleauth.tsimport { 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; - Publishes the Route Middleware at
app/middleware/api_key_middleware.ts:app/middleware/api_key_middleware.tsimport 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(); } } - Registers Router and Named Middleware in
start/kernel.ts:start/kernel.tsimport 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:
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.
