@nowarajs/elysia-jwt
    Preparing search index...

    Function jwt

    • The elysiaJwtPlugin provides secure JSON Web Token (JWT) authentication capabilities for Elysia applications.

      This plugin leverages the industry-standard jose library to ensure robust JWT handling with modern cryptographic standards.

      Type Parameters

      • const TJWTKeyName extends string = "jwt"

        The name to use for JWT functionality in the Elysia context (defaults to 'jwt')

      Parameters

      Returns Elysia<
          "",
          {
              decorator: {
                  [K in string]: {
                      [name in string]: {
                          sign(
                              additionalPayload?: JWTPayload,
                              expiration?: string | number | Date,
                          ): Promise<string>;
                          verify(token?: string): Promise<false | JWTVerifyResult<JWTPayload>>;
                      }
                  }[K]
              };
              derive: {};
              resolve: {};
              store: {};
          },
          { error: {}; typebox: {} },
          { macro: {}; macroFn: {}; parser: {}; schema: {}; standaloneSchema: {} },
          {},
          { derive: {}; resolve: {}; schema: {}; standaloneSchema: {} },
          { derive: {}; resolve: {}; schema: {}; standaloneSchema: {} },
      >

      An Elysia plugin that adds JWT functionality to the application context

      (HttpError) When the secret key is missing or invalid

      Basic usage

      const app = new Elysia()
      .use(jwt({
      secret: process.env.JWT_SECRET!,
      exp: '1h'
      }))
      .post('/login', ({ jwt }) => {
      return jwt.sign({ userId: 123, role: 'user' });
      })
      .get('/protected', async ({ jwt, headers }) => {
      const token = headers.authorization?.replace('Bearer ', '');
      const payload = await jwt.verify(token);
      if (!payload) throw new Error('Invalid token');
      return { user: payload };
      });

      Custom context key name

      const app2 = new Elysia()
      .use(jwt({
      jwtKeyName: 'auth',
      secret: 'my-secret'
      }))
      .post('/login', ({ auth }) => auth.sign({ id: 1 }));