@fusionauth/angular-sdk

中文文档
2.1.0发布于 2026/08/06ApacheTypeScriptES Module构建来源已验证近 30 天下载 3.1万

An SDK for using FusionAuth in Angular applications.

原始使用文档
文档目录11 节
  1. Getting Started
  2. Quickstart
  3. Installation
  4. Configuring FusionAuthModule
  5. Usage
  6. FusionAuthService
  7. Pre-built buttons
  8. DPoP Mode
  9. Known Issues
  10. Documentation
  11. Releases

An SDK for using FusionAuth in Angular applications.

Table of Contents

Overview

This SDK helps manage authentication state for your Angular app and provides functionality to login, register, and logout users. It also can be configured to automatically manage your refresh token.

Your users will be sent to FusionAuth’s themeable hosted login pages and then log in. After that, they are sent back to your Angular application.

Once authentication succeeds, the following secure, HTTP-only cookies will be set:

  • app.at - an OAuth Access Token

  • app.rt - a Refresh Token used to obtain a new app.at. This cookie will only be set if refresh tokens are enabled on your FusionAuth instance.

The access token can be presented to APIs to authorize the request and the refresh token can be used to get a new access token.

There are 2 ways to interact with this SDK:

  1. By hosting your own server that performs the OAuth token exchange and meets the server code requirements for FusionAuth Web SDKs.
  2. By using the server hosted on your FusionAuth instance, i.e., not writing your own server code.

If you are hosting your own server, see server code requirements.

You can use this library against any version of FusionAuth or any OIDC compliant identity server.

Getting Started

If you are new to Angular development, you may want to start with the Quickstart guide. If you are already familiar with Angular development, skip to the Installation section.

Quickstart

See the FusionAuth Angular Quickstart for a full tutorial on using FusionAuth and Angular.

Installation

NPM:

npm install @fusionauth/angular-sdk

Yarn:

yarn add @fusionauth/angular-sdk

Configuring FusionAuthModule

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { FusionAuthModule } from '@fusionauth/angular-sdk';

@NgModule({
  declarations: [],
  imports: [
    FusionAuthModule.forRoot({
      clientId: '', // Your FusionAuth client ID
      serverUrl: '', // The base URL of the server that performs the token exchange
      redirectUri: '', // The URI that the user is directed to after the login/register/logout action
      shouldAutoRefresh: true // option to configure the SDK to automatically handle token refresh. Defaults to false if not specified here.
      // useDpop: true, // Opt-in to DPoP mode. See "DPoP Mode" below. Defaults to false.
    }),
  ],
  providers: [],
  bootstrap: []
})
export class AppModule { }

Usage

FusionAuthService

The injectable FusionAuthService class provides observable properties to which components may subscribe.

Note, you can also use the non-observable getUserInfo method if you wish to implement your observables.

class AppComponent implements OnInit, OnDestroy {
  private fusionAuthService: FusionAuthService = inject(FusionAuthService);

  isLoggedIn: boolean = this.fusionAuthService.isLoggedIn();
  userInfo: UserInfo | null = null;
  isGettingUserInfo: boolean = false;
  subscription?: Subscription;

  ngOnInit(): void {
    if (this.isLoggedIn) {
      this.subscription = this.fusionAuthService
        .getUserInfoObservable({
          onBegin: () => (this.isGettingUserInfo = true),
          onDone: () => (this.isGettingUserInfo = false),
        })
        .subscribe({
          next: (userInfo) => (this.userInfo = userInfo),
          error: (error) => console.error(error),
        });
    }
  }

  ngOnDestroy(): void {
    this.subscription?.unsubscribe();
  }
}

Pre-built buttons

There are three pre-styled buttons that are configured to perform login/logout/registration. They can be placed anywhere in your app as is.

import { Component } from '@angular/core';

@Component({
  selector: 'app-login',
  template: `<fa-login></fa-login>`,
  styleUrls: []
})
export class LoginComponent {}

@Component({
  selector: 'app-logout',
  template: `<fa-logout></fa-logout>`,
  styleUrls: []
})
export class LogoutComponent {}

@Component({
  selector: 'app-register',
  template: `<fa-register></fa-register>`,
  styleUrls: []
})
export class RegisterComponent {}

State parameter

The startLogin and startRegistration functions both accept an optional string parameter called state. The login and register components can also be passed the state as an input. The state that is passed in to the function call will be echoed back in the state query parameter of the callback uri specified in redirectUri on the FusionAuthConfig used to initialize the FusionAuthModule. Though you may pass any value you would like for the state parameter, it is often used to indicate which page the user was on before redirecting to login or registration, so that the user can be returned to that location after a successful authentication.

SSR

The SDK supports Angular applications using SSR. No additional configuration is needed.

DPoP Mode

By default, the SDK calls a Hosted Backend that stores tokens in HttpOnly cookies (useDpop: false, the default). In DPoP mode, the SDK instead calls FusionAuth endpoints directly and binds tokens to a private key generated in the browser. Enable it by setting useDpop: true on FusionAuthConfig:

FusionAuthModule.forRoot({
  clientId: '',
  serverUrl: '',
  redirectUri: '',
  useDpop: true, // Opt-in to DPoP mode.
  dpopTokenStorage: 'localStorage', // 'localStorage' (default, persists across reloads) or 'memory'.
}),

When useDpop: true, FusionAuthService additionally exposes dpopFetch(), generateProof(), and getAccessToken(). These throw a descriptive error if called when useDpop is not enabled.

class AppComponent {
  private fusionAuthService: FusionAuthService = inject(FusionAuthService);

  async callApi() {
    // Recommended — handles attaching DPoP headers (and nonce retries) automatically.
    const response = await this.fusionAuthService.dpopFetch('https://api.example.com/data', { method: 'GET' });

    // For axios or other HTTP libraries that can't use dpopFetch.
    const accessToken = this.fusionAuthService.getAccessToken();
    const proof = await this.fusionAuthService.generateProof('https://api.example.com/data', 'GET', accessToken ?? undefined);
    // axios.get('https://api.example.com/data', {
    //   headers: { Authorization: `DPoP ${accessToken}`, DPoP: proof }
    // });
  }
}

Known Issues

None.

Documentation

Full library documentation

Use backticks for code in this readme. This readme is included on the FusionAuth website, and backticks show the code in the best light there.

Releases

To perform a release to NPM, create a release on GitHub. That will automatically publish a release to GitHub.

版本与发布记录

@fusionauth/angular-sdk 共发布 16 个版本,首次发布于 2023/04/08;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.0latest2026/08/063 个月前
2.0.02026/06/244 个月前
1.3.02024/06/282 年前
1.2.02024/06/262 年前
1.1.02024/06/112 年前
1.0.22024/05/092 年前

关于 @fusionauth/angular-sdk 的常见问题

@fusionauth/angular-sdk 是什么?

An SDK for using FusionAuth in Angular applications.

如何安装 @fusionauth/angular-sdk?

在项目目录运行 npm install @fusionauth/angular-sdk,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(>=22.0.0)、@angular/core(>=22.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fusionauth/angular-sdk 支持 TypeScript 吗?

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

@fusionauth/angular-sdk 支持 ESM 还是 CommonJS?

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

@fusionauth/angular-sdk 可以商用吗?

@fusionauth/angular-sdk 使用 Apache 许可。声明的许可为 Apache,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@fusionauth/angular-sdk 的下载量有多少?

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

@fusionauth/angular-sdk 还在维护吗?

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