@elfsquad/authentication

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

The authentication library allows you to easily authenticate with the Elfsquad API.

原始使用文档
文档目录4 节
  1. Options
  2. Methods
  3. Examples
  4. BFF pattern (secure refresh token storage)

Elfsquad Authentication Library

The authentication library allows you to easily authenticate with the Elfsquad API.

Options

  • clientId identifier of your OpenIdClient that can be obtained in the integrations page of your Elfsquad Management System.
  • redirectUri callback entry point of your app.
  • scope (optional) Requested authentication scope. Defaults to Elfskot.Api offline_access.
  • loginUrl (optional) URL of the authentication service. Defaults to https://login.elfsquad.io.
  • responseMode (optional) OAuth response mode, either 'fragment' or 'query'. Defaults to 'fragment'.
  • storeRefreshToken (optional) Callback to store the refresh token server-side. When provided, the library will call this instead of saving the token to localStorage. Must be provided together with refreshAccessToken and revokeRefreshToken.
  • refreshAccessToken (optional) Callback to refresh the access token via a server-side endpoint. When provided, the library will call this instead of using the built-in localStorage-based refresh flow. Must be provided together with storeRefreshToken and revokeRefreshToken.
  • revokeRefreshToken (optional) Callback to revoke the server-side refresh token on sign-out. Must be provided together with storeRefreshToken and refreshAccessToken.

Methods

  • signIn starts the authentication flow.
  • onSignIn returns a promise that is called after the authentication flow has run successfully.
  • isSignedIn returns a promise with a boolean result that indicates if the user is signed in.
  • getAccessToken returns a promise that resolves into access token.

Examples

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 pattern (secure refresh token storage)

Use the storeRefreshToken, refreshAccessToken, and revokeRefreshToken callbacks to move refresh tokens out of localStorage into server-side HttpOnly cookies, eliminating XSS exposure of long-lived credentials.

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 是什么?

The authentication library allows you to easily authenticate with the 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 和提交记录判断。