Installation
Prerequisites
The guard relies on the AdonisJS auth package. Install it first if your application doesn't use it yet:
node ace add @adonisjs/authInstall the package
npm i @maximemrf/adonisjs-jwtThen run the configure command:
node ace configure @maximemrf/adonisjs-jwtnode ace add @maximemrf/adonisjs-jwt does both steps at once.
The command asks whether you want to use refresh tokens, then:
- adds a
jwtguard toconfig/auth.ts. It doesn't change thedefaultguard; - with refresh tokens:
- adds a
refreshTokensprovider toapp/models/user.ts, - creates the migration of the
jwt_refresh_tokenstable;
- adds a
- prints the next steps.
It keeps the style of your files and leaves existing code untouched: running it again doesn't duplicate anything. When a file doesn't have the expected shape, it prints the code to add yourself.
Pass --refresh-tokens or --no-refresh-tokens to skip the question, for example in a script:
node ace configure @maximemrf/adonisjs-jwt --no-refresh-tokensWith refresh tokens, run the migration:
node ace migration:runAdonisJS v6
Install the 0.7.x version, the last one that supports AdonisJS v6:
npm i @maximemrf/adonisjs-jwt@0.7.1The rest of this documentation describes the latest version.
Start from an example
Two ready-to-run apps use the package, with tests:
- adonisjs-jwt-starter: the AdonisJS API starter kit with JWT access tokens and refresh tokens instead of opaque tokens, with signup, login, refresh, logout and a protected route.
- adonisjs-jwt-k8s-example: an API that authenticates Kubernetes ServiceAccount tokens in JWKS mode, to deploy in your cluster with a single
kubectl apply -k. See Kubernetes ServiceAccount tokens.
Next step
Configure the guard in config/auth.ts.