@dfinity/auth-client

中文文档
3.4.3发布于 2025/11/20Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 2.3万

JavaScript and TypeScript library to provide a simple integration with an IC Internet Identity

此软件包已被标记为弃用

This package has been deprecated. Its functionality is now part of `@icp-sdk/auth` (https://js.icp.build/auth), under the `client` submodule.

原始使用文档
文档目录5 节
  1. Installation
  2. In the browser:
  3. Storage and Key management
  4. Idle Management
  5. IdleManager Example Usage

@dfinity/auth-client

Simple interface to get your web application authenticated with the Internet Identity Service

Do you want to know more about developing on the Internet Computer? Visit the Developer Docs.

Additional API Documentation can be found here.

Still using @dfinity/auth-client? Migrate to @icp-sdk/auth!


Installation

Using AuthClient:

npm i --save @dfinity/auth-client

In the browser:

import { AuthClient } from '@dfinity/auth-client';

To get started with auth client, run

const authClient = await AuthClient.create();

The authClient can log in with

authClient.login({
  // 7 days in nanoseconds
  maxTimeToLive: BigInt(7 * 24 * 60 * 60 * 1000 * 1000 * 1000),
  onSuccess: async () => {
    handleAuthenticated(authClient);
  },
});

It opens an identity.ic0.app window, saves your delegation to localStorage, and then sets you up with an identity.

Then, you can use that identity to make authenticated calls using the @dfinity/agent Actor.

const identity = await authClient.getIdentity();
const actor = Actor.createActor(idlFactory, {
  agent: new HttpAgent({
    identity,
  }),
  canisterId,
});

Storage and Key management

If you prefer not to use ECDSA keys or the default IndexedDb storage interface, you can provide your own. Some reasons to use a custom storage implementation might be

  • You prefer to use LocalStorage
  • You don't want to persist keys across page loads for heightened security
  • You have an alternate strategy for identity management

There is an exported LocalStorage interface, but any structure that implements the AuthClientStorage interface will work.

export type StoredKey = string | CryptoKeyPair;
export interface AuthClientStorage {
  get(key: string): Promise<StoredKey | null>;

  set(key: string, value: StoredKey): Promise<void>;

  remove(key: string): Promise<void>;
}

So you could easily implement your own

const noStorageImpl = {
  get(key: string) {
    return Promise.resolve(null);
  },
  set(key: string, value: StoredKey) {
    return Promise.resolve();
  },
  remove(key: string) {
    return Promise.resolve();
  },
};
const authClient = await AuthClient.create({
  storage: noStorageImpl,
});

If you are using a custom storage implementation like LocalStorage that only supports strings, you should use the keyType option to use an Ed25519 key instead of the default ECDSA key.

const authClient = await AuthClient.create({
  storage: new LocalStorage(),
  keyType: 'Ed25519',
});

Idle Management

The AuthClient provides two forms of security for session management. The first is built into the Internet Identity delegation - the maxTimeToLive option in nanoseconds determines how long the DelegationIdentity you get back will be valid for. The second is the Idle Manager, which moniters keyboard, mouse and touchscreen identity. The Idle Manager will automatically log you out if you don't interact with the browser for a period of time.

If you pass no options to the IdleManager, it will log you out after 10 minutes of inactivity by removing the DelegationIdentity from localStorage and then calling window.location.reload().

If you pass an onIdle option, it will call that function when the user is idle, replacing the default window.location.reload() behavior. You can also register callbacks after the idleManager is created with the idleManager.registerCallback() method, which will also replace the default callback.

The full set of options for the IdleManager is:

  /**
   * Callback after the user has gone idle
   */
  onIdle?: IdleCB;
  /**
   * timeout in ms
   * @default 30 minutes [600_000]
   */
  idleTimeout?: number;
  /**
   * capture scroll events
   * @default false
   */
  captureScroll?: boolean;
  /**
   * scroll debounce time in ms
   * @default 100
   */
  scrollDebounce?: number;

Additionally, the AuthClient accepts a couple additional flags to idleOptions to control the IdleManager:

  /**
   * Disables idle functionality for {@link IdleManager}
   * @default false
   */
  disableIdle?: boolean;

  /**
   * Disables default idle behavior - call logout & reload window
   * @default false
   */
  disableDefaultIdleCallback?: boolean;

IdleManager Example Usage

const authClient = await AuthClient.create({
  idleOptions: {
    idleTimeout: 1000 * 60 * 30, // set to 30 minutes
    disableDefaultIdleCallback: true // disable the default reload behavior
  }
});
// ...authClient.login()
const identity = await authClient.getIdentity();
const actor = Actor.createActor(idlFactory, {
  agent: new HttpAgent({
    identity,
  }),
  canisterId,
});

refreshLogin() {
  // prompt the user then refresh their authentication
  authClient.login({
    onSuccess: async () => {
      const newIdentity = await AuthClient.getIdentity();
      Actor.getAgent(actor).replaceIdentity(newIdentity);
    }
  });
}

authClient.idleManager?.registerCallback?.(refreshLogin);

In this code, we create an authClient with an idle timeout of 30 minutes. When the user is idle, we invalidate their identity and prompt them to login again.

After the user logs in, we can set the new identity in the actor without reloading the page.

Note: depends on @dfinity/agent and @dfinity/identity.

版本与发布记录

@dfinity/auth-client 共发布 101 个版本,首次发布于 2021/04/30;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.4.3latest2025/11/2011 个月前
3.4.22025/11/111 年前
3.4.12025/10/241 年前
3.4.02025/10/221.1 年前
3.3.12025/10/211.1 年前
3.3.02025/10/141.1 年前

其他发布通道:react-native → 0.19.3-react-native,beta → 3.2.0-beta.0。

关于 @dfinity/auth-client 的常见问题

@dfinity/auth-client 是什么?

JavaScript and TypeScript library to provide a simple integration with an IC Internet Identity注意:该包已被作者标记为弃用——This package has been deprecated. Its functionality is now part of `@icp-sdk/auth` (https://js.icp.build/auth), under the `client` submodule.

如何安装 @dfinity/auth-client?

在项目目录运行 npm install @dfinity/auth-client,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@dfinity/agent(3.4.3)、@dfinity/identity(3.4.3)、@dfinity/principal(3.4.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dfinity/auth-client 支持 TypeScript 吗?

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

@dfinity/auth-client 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dfinity/auth-client 可以商用吗?

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

@dfinity/auth-client 的下载量有多少?

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

@dfinity/auth-client 还在维护吗?

最新版本 3.4.3 发布于 2025/11/20(11 个月前);至今共发布 101 个版本,其中过去 12 个月发布了 1 个;首次发布于 2021/04/30。作者已将其标记为弃用,新项目建议寻找替代方案。