Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@elysiajs/jwt

elysiajs81.5kMIT1.4.0TypeScript support: included

Plugin for Elysia for using JWT Authentication

elysia, jwt, auth, authentication

readme

@elysiajs/jwt

Plugin for Elysia for using JWT Authentication.

Installation

bun add @elysiajs/jwt

Example

import { Elysia, t } from 'elysia';
import { jwt } from '@elysiajs/jwt';

const app = new Elysia()
  .use(
    jwt({
      name: 'jwt',
      // This should be Environment Variable
      secret: 'MY_SECRETS',
    })
  )
  .get('/sign/:name', async ({ jwt, cookie: { auth }, params }) => {
    auth.set({
      value: await jwt.sign(params),
      httpOnly: true,
    });

    return `Sign in as ${params.name}`;
  })
  .get('/profile', async ({ jwt, set, cookie: { auth } }) => {
    const profile = await jwt.verify(auth);

    if (!profile) {
      set.status = 401;
      return 'Unauthorized';
    }

    return `Hello ${profile.name}`;
  })
  .listen(8080);

Config

This package extends jose, most config is inherited from Jose.

Below are configurable properties for using JWT plugin

name

Name to decorate method as:

For example, jwt will decorate Context with Context.jwt

secret

JWT secret key

schema

Type strict validation for JWT payload

Jose's config

Below is the config inherits from jose

alg

@default 'HS256'

Algorithm to sign JWT with

crit

Critical Header Parameter.

iss

JWT Issuer

@see RFC7519#section-4.1.1

sub

JWT Subject

@see RFC7519#section-4.1.2

aud

JWT Audience

@see RFC7519#section-4.1.3

jti

JWT ID

@see RFC7519#section-4.1.7

nbf

JWT Not Before

@see RFC7519#section-4.1.5

exp

JWT Expiration Time

@see RFC7519#section-4.1.4

iat

JWT Issued At

@see RFC7519#section-4.1.6

changelog

1.3.3 - 14 Jul 2025

Feature:

  • #110 add optional, typed 'options' argument to jwt.verify() and pass to jose jwtVerify() if present

Bug fix:

  • #109 type inconsistencies in JWT payload and improve schema-based inference
  • #107 correct iat claim handling and expand allowed claim value type
  • #105 adjust README in regard to the cookie plugin

1.3.2 - 14 Jul 2025

Bug fix:

  • #104 Resolve various issues with payload, schema, and types
  • #101 Closes Config exp not encoded in payload
  • #23 Closes bug sign args exp type
  • #98 Closes verify fails when using schema in Elysia JWT plugin
  • #52 Closes iat Type inconsistency
  • #36 Closes upgrade dependency "jose" to latest version to fix issues
  • #19 Closes Header Parameter is stored in the payload instead of header

1.3.1 - 31 May 2025

Feature:

  • #100 allow dynamic exp/nbf in sign method

1.3.0-exp.0 - 23 Apr 2025

Change:

  • Add support for Elysia 1.3

1.2.0-rc.0 - 23 Dec 2024

Change:

  • Add support for Elysia 1.2

1.1.1 - 5 Sep 2024

Feature:

  • add provenance publish

1.1.0 - 16 Jul 2024

Change:

  • Add support for Elysia 1.1

1.1.0-rc.0 - 12 Jul 2024

Change:

  • Add support for Elysia 1.1

1.0.2 - 18 Mar 2024

Change:

  • Add support for Elysia 1.0

1.0.0 - 16 Mar 2024

Change:

  • Add support for Elysia 1.0

1.0.0-rc.0 - 1 Mar 2024

Change:

  • Add support for Elysia 1.0

1.0.0-beta.1 - 17 Feb 2024

Change:

  • Add support for Elysia 1.0

1.0.0-beta.0 - 6 Feb 2024

Change:

  • Add support for Elysia 1.0

0.8.0 - 23 Dec 2023

Change:

  • Add support for Elysia 0.8

0.8.0-rc.0 - 15 Dec 2023

Change:

  • Add support for Elysia 0.8

0.7.1 - 15 Dec 2023

  • Minimum support for TypeScript to >= 5.0
  • Minimum support for Elysia >= 0.7.20

0.7.0 - 20 Sep 2023

  • Add support for Elysia 0.7

0.7.0-beta.0 - 18 Sep 2023

  • Add support for Elysia 0.7

0.6.4 - 11 Sep 2023

-#11 Remove unused imports -#10 Meaningful type import -#9 s Add missing ESLint plugins -#8 Reformat CHANGELOG with -#7 Format README with Markdownlint -#6 Correct the schema validation -#5 Support KeyLike and Uint8Array as secret -#4 Remove unnecessary "as any" -#3 Schema alidator Security Fix

0.6.0 - 6 Aug 2023

  • Add support for Elysia 0.6

0.6.0-rc.0 - 6 Aug 2023

  • Add support for Elysia 0.6

0.5.0 - 15 May 2023

  • Add support for Elysia 0.5
  • Add CommonJS support

0.3.0 - 29 Mar 2023

Fix:

  • Update to support Elysia 0.3

0.2.1 - 17 Feb 2023

Fix:

  • JWT namespace wrong infer type

0.2.0 - 17 Feb 2023

Improvement:

  • Add support for Elysia >= 0.2.0

0.1.0-rc.4 - 19 Dec 2022

Improvement:

  • Make name optional

0.1.0-rc.3 - 13 Dec 2022

Improvement:

  • Add support for Elysia 0.1.0-rc.5

0.1.0-rc.2 - 9 Dec 2022

Fix:

  • Add main fields Bundlephobia

0.1.0-rc.1 - 6 Dec 2022

Improvement:

  • Support for Elysia 0.1.0-rc.1 onward

0.0.0-experimental.1 - 30 Oct 2022

Change:

  • Support for KingWorld 0.0.0-experimental.28 onward
  • chore: update dependencies