@antelopejs/auth-jwt

中文文档
1.1.0发布于 2026/11/05Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 4398

JWT authentication module for AntelopeJS, implementing the Auth interface with jsonwebtoken

原始使用文档
文档目录8 节
  1. Interfaces
  2. Install
  3. Configuration
  4. Quick start
  5. Documentation
  6. Compatibility
  7. Contributing
  8. License

@antelopejs/auth-jwt

npm License Discord Documentation

The AntelopeJS authentication module. It implements @antelopejs/interface-auth and signs and verifies its tokens as JSON Web Tokens with jsonwebtoken.

Part of AntelopeJS. Used by AntelopeJS DMS, the open-source back office for your PostgreSQL, MongoDB or RethinkDB database.

Interfaces

InterfaceDocumentation
@antelopejs/interface-authAuth

The interface pages document the @Authentication decorator, custom auth decorators, token sources, and cookies. The module also depends on @antelopejs/interface-api, which @antelopejs/api implements.

Install

ajs project modules add @antelopejs/auth-jwt
ajs project modules install

ajs project modules install adds @antelopejs/api when no module of the project implements the API interface yet.

Configuration

Sign with a shared secret:

import { defineConfig } from "@antelopejs/interface-core/config";

export default defineConfig({
  name: "my-project",
  modules: {
    "auth-jwt": {
      source: {
        type: "package",
        package: "@antelopejs/auth-jwt",
        version: "^2.0.0",
      },
      config: {
        secret: "change-me",
        signOptions: { expiresIn: "1h" },
        verifyOptions: { ignoreExpiration: false },
      },
    },
  },
  envOverrides: {
    JWT_SECRET: "modules.auth-jwt.config.secret",
  },
});

When the JWT_SECRET environment variable is set, envOverrides writes its value into secret.

To sign with a key pair instead, give the paths of the two key files. The module reads both files when it constructs.

config: {
  signKey: "./keys/private.pem",
  verifyKey: "./keys/public.pem",
  signOptions: { algorithm: "RS256", expiresIn: "1h" },
  verifyOptions: { algorithms: ["RS256"] },
},
KeyDescription
secretShared secret that signs and verifies tokens. Takes precedence over signKey and verifyKey.
signKeyPath of the private key file. The module uses it with verifyKey when secret is not set.
verifyKeyPath of the public key file.
signOptionsDefault jsonwebtoken sign options. The expiresIn and notBefore that SignRaw or SignServerResponse receive override them.
verifyOptionsDefault jsonwebtoken verify options. Options passed to ValidateRaw override them.

The module refuses to start without secret or both signKey and verifyKey, and throws Invalid JWT config. ajs project modules add writes secret: "your-secret-key". Replace it before you deploy.

Quick start

In the module that serves the routes, add both interfaces.

pnpm add @antelopejs/interface-api @antelopejs/interface-auth

Issue a token as a cookie, then read it back on a protected route.

import {
  Context,
  Controller,
  Get,
  Post,
  type RequestContext,
} from "@antelopejs/interface-api";
import { Authentication, SignServerResponse } from "@antelopejs/interface-auth";

interface Session {
  userId: string;
}

export class SessionController extends Controller("/session") {
  @Post("/login")
  async login(@Context() context: RequestContext) {
    await SignServerResponse(
      context.rawResponse,
      { userId: "42" },
      { expiresIn: "1h" },
      { httpOnly: true, path: "/" },
    );
    return { ok: true };
  }

  @Get("/me")
  me(@Authentication() session: Session) {
    return session;
  }
}

GET /session/me reads the token from the x-antelopejs-auth header, or from the ANTELOPEJS_AUTH cookie when the header is absent. It answers 401 when the token is missing. It answers 403 when the token is invalid or expired, or when its payload is neither a string nor a JSON object, such as an array or a number.

Documentation

  • Auth documents the @antelopejs/interface-auth contract that this module implements.
  • Configuration documents module sources, config blocks, and config variables in antelope.config.ts.

Compatibility

PackageSupportedTested with
@antelopejs/interface-api>=0.2.0 <1.0.00.2.0
@antelopejs/interface-auth>=0.2.0 <0.3.00.2.0
@antelopejs/interface-core>=0.4.0 <1.0.00.4.0

The generated table lists the interface packages and ranges this module loads, read from package.json.

Contributing

pnpm install
pnpm dev            # run the playground/ project in watch mode
pnpm lint
pnpm format:check
pnpm knip
pnpm test           # build, then ajs module test

Read the contribution guidelines before opening a pull request.

License

Apache-2.0

版本与发布记录

@antelopejs/auth-jwt 共发布 12 个版本,首次发布于 2025/05/09;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2026/11/051 天前
2.0.02026/11/051 天前
1.0.42026/10/1719 天前
1.0.32026/08/302 个月前
1.0.22026/08/212 个月前
1.0.12026/06/164 个月前

关于 @antelopejs/auth-jwt 的常见问题

@antelopejs/auth-jwt 是什么?

JWT authentication module for AntelopeJS, implementing the Auth interface with jsonwebtoken

如何安装 @antelopejs/auth-jwt?

在项目目录运行 npm install @antelopejs/auth-jwt,或使用 pnpm add / yarn add / bun add。

@antelopejs/auth-jwt 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@antelopejs/auth-jwt 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@antelopejs/auth-jwt 可以商用吗?

@antelopejs/auth-jwt 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@antelopejs/auth-jwt 的下载量有多少?

根据 npm 官方下载统计,@antelopejs/auth-jwt 在 2026/10/06 – 2026/11/04 这 30 天内被下载 4,398 次,日均约 147 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@antelopejs/auth-jwt 还在维护吗?

最新版本 1.1.0 发布于 2026/11/05(1 天前);至今共发布 12 个版本,其中过去 12 个月发布了 9 个;首次发布于 2025/05/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。