@elfsquad/authentication

中文文档
3.1.1发布于 2026/08/25MITTypeScriptCommonJS构建来源已验证近 30 天下载 3523

认证库可以方便地认证Elfsquad API.

中文使用文档
文档目录4 节
  1. 选项
  2. 方法
  3. 实例
  4. BFF 模式( 安全刷新令牌存储)

Elfsquad 认证库

认证库可以方便地认证Elfsquad API.

选项

  • clientId 在您的整合页面中获取的 OpenIdClient 标识符 精灵管理系统。 。 。 .
  • redirectUri 您的应用程序的回调输入点 .
  • scope (可选)请求认证范围. 默认为 Elfskot.Api offline_access。 。 。 .
  • loginUrl (可选)认证服务的URL. 默认为 https://login.elfsquad.io。 。 。 .
  • responseMode (可选) OAuth 响应模式, 也可以 'fragment' 或者说 'query'默认为 'fragment'。 。 。 .
  • storeRefreshToken (可选)召回存储刷新令牌服务器侧. 当提供时,库会调用此词,而不是将符号保存到 localStorage。必须同时提供 refreshAccessToken 财务报告和审定财务报表 revokeRefreshToken。 。 。 .
  • refreshAccessToken (可选的) 回调通过服务器端端点刷新访问令牌 。 提供后, 库会调用它, 而不是使用内置 localStorage-基于新鲜流。 必须与 storeRefreshToken 财务报告和审定财务报表 revokeRefreshToken。 。 。 .
  • revokeRefreshToken (可选的) 回调以取消在签出时服务器侧刷新令牌 。 必须与 storeRefreshToken 财务报告和审定财务报表 refreshAccessToken。 。 。 .

方法

  • signIn 启动认证流程.
  • onSignIn 返回在认证流程成功运行后调用的承诺.
  • isSignedIn 返回带有布尔结果的保证,该结果表明用户是否被签名.
  • getAccessToken 返回解析为访问符的许诺.

实例

import { AuthenticationContext } from "@elfsquad/authentication";

var authenticationContext = new AuthenticationContext({
  clientId: "c2a349a9-02ea-4e1e-a59d-65870529f713",
  redirectUri: "https://example.com",
});

authenticationContext
  .onSignIn()
  .then(() => {
    authenticationContext.getAccessToken().then((accessToken) => {
      console.log("accessToken", accessToken);
    });
  })
  .catch((error) => {
    console.error(error);
  });

authenticationContext.isSignedIn().then((isSignedIn) => {
  if (!isSignedIn) {
    authenticationContext.signIn();
  }
});

BFF 模式( 安全刷新令牌存储)

使用 storeRefreshToken, (中文(简体) ), refreshAccessToken,以及 revokeRefreshToken 调用将刷新符移出 localStorage 进入服务器侧 HttpOnly Cookie,消除XSS曝光的长寿证书.

import { AuthenticationContext } from "@elfsquad/authentication";

const authenticationContext = new AuthenticationContext({
  clientId: "c2a349a9-02ea-4e1e-a59d-65870529f713",
  redirectUri: "https://example.com",
  storeRefreshToken: (token) =>
    fetch("/auth/store-token", {
      method: "POST",
      body: JSON.stringify({ token }),
    }).then(() => {}),
  refreshAccessToken: () => fetch("/auth/refresh").then((r) => r.json()),
  revokeRefreshToken: () =>
    fetch("/auth/revoke", { method: "POST" }).then(() => {}),
});

authenticationContext.onSignIn().then(() => {
  authenticationContext.getAccessToken().then((accessToken) => {
    console.log("accessToken", accessToken);
  });
});

版本与发布记录

@elfsquad/authentication 共发布 28 个版本,首次发布于 2021/08/14;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.1.1latest2026/08/252 个月前
3.0.02026/04/037 个月前
2.0.102023/09/143 年前
2.0.92023/04/193 年前
2.0.82023/01/053 年前
2.0.72023/01/033 年前

关于 @elfsquad/authentication 的常见问题

@elfsquad/authentication 是什么?

认证库可以方便地认证Elfsquad API.

如何安装 @elfsquad/authentication?

在项目目录运行 npm install @elfsquad/authentication,或使用 pnpm add / yarn add / bun add。

@elfsquad/authentication 支持 TypeScript 吗?

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

@elfsquad/authentication 支持 ESM 还是 CommonJS?

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

@elfsquad/authentication 可以商用吗?

@elfsquad/authentication 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elfsquad/authentication 的下载量有多少?

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

@elfsquad/authentication 还在维护吗?

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