adonisjs-jwt

3rd Party
Authentication with JWT token for AdonisJS
Authentication
Created Mar 16, 2024 Updated Nov 14, 2026

@maximemrf/adonisjs-jwt

Download Version NPM Last Update License AdonisV7

AdonisJS package to authenticate users using JWT tokens.

📖 Documentation: maximemrf.github.io/adonisjs-jwt

  • Access tokens signed with HMAC, RSA or ECDSA, or verified with JWKS
  • Rotating refresh tokens stored in the database
  • Bearer header or HttpOnly cookies
  • Works with the auth middleware and Japa's loginAs

Compatibility

Package version AdonisJS version Node.js
0.7.x AdonisJS v6 >= 20.6.0
>= 0.8.x AdonisJS v7 >= 24.0.0

Upgrading from 0.9? Read the 1.0 upgrade guide.

Installation

node ace add @adonisjs/auth # if not installed yet
npm i @maximemrf/adonisjs-jwt
node ace configure @maximemrf/adonisjs-jwt

Quick start

Register the guard in config/auth.ts:

import { defineConfig } from '@adonisjs/auth'
import { sessionUserProvider } from '@adonisjs/auth/session'
import { jwtGuard } from '@maximemrf/adonisjs-jwt'

const authConfig = defineConfig({
  default: 'jwt',
  guards: {
    jwt: jwtGuard({
      provider: sessionUserProvider({
        model: () => import('#models/user'),
      }),
    }),
  },
})

export default authConfig

Issue a token and protect routes:

import router from '@adonisjs/core/services/router'
import { middleware } from '#start/kernel'
import User from '#models/user'

router.post('login', async ({ request, auth }) => {
  const { email, password } = request.only(['email', 'password'])
  const user = await User.verifyCredentials(email, password)
  return await auth.use('jwt').generate(user)
})

router.get('me', async ({ auth }) => auth.getUserOrFail()).use(middleware.auth())

Then see the documentation for refresh tokens, cookies, asymmetric keys, JWKS and security.

Examples

Contributing

npm install
npm test          # lint, tests and coverage
npm run docs:dev  # documentation, on http://localhost:5173/adonisjs-jwt/

License

MIT