Getting Started

Configuration

Configure the HTTP header name and API key value in config/simpleauth.ts.

The configuration for @eienjs/adonisjs-simple-auth is stored in the config/simpleauth.ts file, which is created when you configure the package.

Default Configuration

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;

Configuration Options

The defineConfig function accepts a SimpleAuthConfig object with the following properties:

OptionTypeDefaultRequiredDescription
apiKeyHeaderstring'x-api-key'NoThe HTTP request header name inspected for the API key. Header lookup in AdonisJS is case-insensitive.
apiKeyValuestring | Secret<string>—YesThe expected API key token. Accepts a plain string or an AdonisJS Secret<string> instance.

apiKeyHeader

By default, the package reads the x-api-key header from incoming HTTP requests. If you omit apiKeyHeader, it defaults to 'x-api-key'.

To use a custom header name, set apiKeyHeader in config/simpleauth.ts:

config/simpleauth.ts
import { defineConfig } from '@eienjs/adonisjs-simple-auth';
import env from '#start/env';

export default defineConfig({
  apiKeyHeader: 'x-service-token',
  apiKeyValue: env.get('API_KEY'),
});

apiKeyValue

The secret token that incoming requests must match. When resolved by defineConfig, any plain string value is automatically wrapped in an AdonisJS Secret instance to prevent accidental exposure in logs or stack traces. If you already pass a Secret<string> (for example, via Env.schema.secret()), the existing Secret instance is preserved.

If apiKeyValue resolves to an empty or whitespace-only string, all authentication attempts will fail with a SimpleAuthException (401 Unauthorized). Always ensure API_KEY is set in your environment.

Environment Schema Recommendation

We recommend validating API_KEY as a secret in start/env.ts so your application fails fast on boot if the key is missing and keeps the value redacted inside a Secret container:

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

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

To generate and manage your API_KEY value, see Generating API Keys.