@meshmakers/shared-auth

中文文档
3.5.550发布于 2026/11/02TypeScriptES Module

OAuth2/OIDC authentication library for Angular applications using angular-oauth2-oidc.

原始使用文档
文档目录19 节
  1. Installation
  2. Quick Start
  3. API Reference
  4. AuthorizeService
  5. Guards
  6. Interceptor
  7. TenantTopologyService
  8. Error classification
  9. UI Components
  10. Observability: session identity (AB#7327)
  11. Configuration Options
  12. User Interface
  13. Roles
  14. Detailed Documentation
  15. Architecture
  16. Entry Points
  17. Dependencies
  18. Build & Test
  19. Documentation and Testing Standards

@meshmakers/shared-auth

OAuth2/OIDC authentication library for Angular applications using angular-oauth2-oidc.

Installation

npm install @meshmakers/shared-auth

Quick Start

// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { provideMmSharedAuth, authorizeInterceptor } from '@meshmakers/shared-auth';

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(withInterceptors([authorizeInterceptor])),
    provideMmSharedAuth(),
  ]
};
// app.component.ts
import { Component, inject, OnInit } from '@angular/core';
import { AuthorizeService } from '@meshmakers/shared-auth';

@Component({...})
export class AppComponent implements OnInit {
  private readonly authorizeService = inject(AuthorizeService);

  async ngOnInit() {
    await this.authorizeService.initialize({
      issuer: 'https://your-identity-provider.com/',
      redirectUri: window.location.origin,
      clientId: 'your-client-id',
      scope: 'openid profile email',
      wellKnownServiceUris: ['https://api.your-service.com']
    });
  }
}

API Reference

AuthorizeService

The main service for authentication.

SignalTypeDescription
isAuthenticatedSignal<boolean>Current authentication status
userSignal<IUser | null>Current user information
accessTokenSignal<string | null>OAuth access token
userInitialsSignal<string | null>User initials (e.g., "JD")
issuerSignal<string | null>OAuth issuer URL
sessionLoadingSignal<boolean>Session loading state
rolesSignal<string[]>User roles (computed)
// Usage in template
@if (authorizeService.isAuthenticated()) {
  <p>Welcome, {{ authorizeService.user()?.name }}</p>
}

// Usage in TypeScript
const isAuth = this.authorizeService.isAuthenticated();
const roles = this.authorizeService.roles();

Methods

MethodDescription
initialize(options)Initialize OAuth with configuration
uninitialize()Clean up OAuth session
login()Start OAuth login flow
logout()Log out user
isInRole(role)Check if user has specific role
getAccessTokenSync()Get current access token synchronously
getServiceUris()Get configured service URIs

Guards

Functional guards for route protection.

import { authorizeGuard, authorizeMatchGuard } from '@meshmakers/shared-auth';

const routes: Routes = [
  // Basic authentication check
  {
    path: 'dashboard',
    component: DashboardComponent,
    canActivate: [authorizeGuard]
  },

  // With role requirement
  {
    path: 'admin',
    component: AdminComponent,
    canActivate: [authorizeGuard],
    data: { roles: ['AdminPanelManagement'] }
  },

  // Lazy-loaded module
  {
    path: 'reports',
    loadChildren: () => import('./reports/routes'),
    canMatch: [authorizeMatchGuard]
  }
];
GuardTypeDescription
authorizeGuardCanActivateFnRoute activation with optional role check
authorizeChildGuardCanActivateFnChild route guard
authorizeMatchGuardCanMatchFnLazy-load matching guard
authorizeDeactivateGuardFunctionAlways allows deactivation

Interceptor

Automatically adds Bearer token to HTTP requests.

import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { authorizeInterceptor } from '@meshmakers/shared-auth';

providers: [
  provideHttpClient(withInterceptors([authorizeInterceptor]))
]

The interceptor adds the Authorization: Bearer <token> header to:

  • Same-origin requests (relative URLs)
  • Requests to configured wellKnownServiceUris

It also recovers from an expired access token: when a request that carried a token comes back 401, the token is refreshed and the request is retried once with the new bearer. The refresh is single-flight (concurrent 401s share one refresh), and a 401 that arrives after that refresh already finished reuses its result instead of starting another one. A retry is never itself retried. Requests without a token, the service health report (/health, which needs the bearer since AB#6739 but is never refreshed on 401) and the token endpoint itself are left alone, and if the refresh fails the original 401 reaches the caller.

TenantTopologyService

await inject(TenantTopologyService).load(rootTenantId) returns which of the user's tenants lie below the app root (topologyLeaves, topologyDirectChildren, topologyTenantIds) or a topologyKnown: false result with a reason; it never throws. Map every unknown result to "token tenant only". See docs/README.md.

Error classification

authorizationRefusal(error) tells a 401 and a 403 apart on an error typed unknown, so each host can phrase the outcome in its own words without writing that status check again.

import { authorizationRefusal } from '@meshmakers/shared-auth';

switch (authorizationRefusal(error)) {
  case 'forbidden': return this.translate.instant('pipeline.forbidden');
  case 'unauthorized': return this.translate.instant('pipeline.unauthorized');
  default: return null;   // not an authorization problem — leave it to the caller
}

The status is read structurally and must be a number, so a rethrown error that lost its HttpErrorResponse prototype still classifies, while a body that merely carries "403" does not.

UI Components

LoginAppBarSectionComponent

A pre-built login/logout component for the app bar. Available via the secondary entry point @meshmakers/shared-auth/login-ui (requires Kendo UI).

import { LoginAppBarSectionComponent } from '@meshmakers/shared-auth/login-ui';

@Component({
  imports: [LoginAppBarSectionComponent],
  template: `
    <mm-login-app-bar-section
      [showRegister]="true"
      (register)="onRegister()">
    </mm-login-app-bar-section>
  `
})

Observability: session identity (AB#7327)

Opt-in: an app can tell its browser monitoring (Dash0 RUM) who is signed in and on which tenant. Apps that do not opt in see no change, and @dash0/sdk-web is not required (it is an optional peer dependency used only by the @meshmakers/shared-auth/dash0 entry point).

// app.config.ts — after the Dash0 SDK was initialised (init()) in main.ts
import { provideMmSharedAuth } from '@meshmakers/shared-auth';
import { provideDash0SessionIdentity } from '@meshmakers/shared-auth/dash0';

providers: [provideMmSharedAuth(), provideDash0SessionIdentity()]
AttributeValue
user.idsub claim
user.nameuser name
user.emaile-mail address (omitted when the identity has none)
octo.tenant.idtenant of the current access token (same key as the backend services)
  • When: set after sign-in and after every tenant switch (in-place token exchange and redirect fallback), only when the identity actually changed (a token refresh does not repeat it); cleared when the session ends (logout, session_terminated, token_refresh_error, cross-tab logout, the reset before a redirect tenant switch). The page load before sign-in stays anonymous.
  • Never breaks auth: the sink is driven from one effect() over the user and token-tenant signals; an exception in it is caught and logged as one console.warn with a fixed text (no name, no e-mail).
  • Privacy: name and e-mail are personal data and are sent to Dash0 (EU region). Only enable it in apps and environments where that is covered by the data protection information (decision 2026-10-22, AB#7326).
  • Other monitoring tools: provide your own SessionIdentitySink ({ set(identity), clear() }) under the SESSION_IDENTITY_SINK token of the main entry point; the Dash0 adapter is just one implementation.

Configuration Options

interface AuthorizeOptions {
  issuer?: string;                    // Identity Provider URL
  redirectUri?: string;               // Redirect after login
  postLogoutRedirectUri?: string;     // Redirect after logout
  clientId?: string;                  // OAuth client ID
  scope?: string;                     // OAuth scopes
  showDebugInformation?: boolean;     // Enable debug logging
  sessionChecksEnabled?: boolean;     // Enable session checks
  wellKnownServiceUris?: string[];    // APIs that should receive auth token
}

User Interface

interface IUser {
  sub: string;                    // Subject (user ID)
  name: string;                   // Display name
  given_name: string | null;      // First name
  family_name: string | null;     // Last name
  email: string | null;           // Email address
  role: string[] | null;          // User roles
  idp: string;                    // Identity provider
}

Roles

Pre-defined role constants:

import { Roles } from '@meshmakers/shared-auth';

if (authorizeService.isInRole(Roles.AdminPanelManagement)) {
  // Admin access
}

Roles.FileViewer / Roles.FileManagement gate the platform file system (browse+download / manage), next to Roles.ReportingViewer / Roles.ReportingManagement (AB#6171 decision D5). They are seeded by System.Identity.Bootstrap; a one-off identity migration grants them to every holder of the matching Reporting role.

Detailed Documentation

See docs/README.md for complete API reference with usage examples.

Architecture

shared-auth/
├── src/                              # Main entry point (@meshmakers/shared-auth)
│   ├── public-api.ts
│   └── lib/
│       ├── authorize.service.ts      # Core OAuth2/OIDC service
│       ├── authorize.guard.ts        # Functional route guards
│       ├── authorize.interceptor.ts  # Functional HTTP interceptor
│       ├── roles.ts                  # Role enum
│       └── mm-login-app-bar-section/ # Component (internal, not exported)
│
└── login-ui/                         # Secondary entry point (@meshmakers/shared-auth/login-ui)
    └── src/
        ├── public-api.ts
        └── mm-login-app-bar-section/ # LoginAppBarSectionComponent (Kendo-dependent)

Entry Points

Entry PointImport PathDescription
Main@meshmakers/shared-authCore auth service, guards, interceptor (no Kendo dependency)
Login UI@meshmakers/shared-auth/login-uiLoginAppBarSectionComponent (requires Kendo UI)
Dash0@meshmakers/shared-auth/dash0provideDash0SessionIdentity() (requires the optional peer @dash0/sdk-web)

Dependencies

  • Angular 21 (core, common/http, router)
  • angular-oauth2-oidc v20 (OAuth2/OIDC client)
  • @progress/kendo-angular-* (optional, only for login-ui entry point)

Build & Test

# Build
npm run build:shared-auth

# Lint
npm run lint:shared-auth

# Run tests
npm run test:shared-auth

Documentation and Testing Standards

  • All developer documentation must be written in English
  • Every code change must include updated documentation — update README.md, CLAUDE.md, docs/README.md, or inline docs when adding, modifying, or removing features
  • Unit tests and integration tests must be executed after every code change
  • Existing tests must be updated when the behavior of tested code changes
  • New tests must be added when new features, components, or services are implemented
  • Never commit code with failing tests

版本与发布记录

@meshmakers/shared-auth 共发布 720 个版本,首次发布于 2022/11/14;过去 12 个月发布了 288 个版本。

版本发布日期距今
3.5.550latest2026/11/021 天前
3.5.5302026/11/012 天前
3.5.5202026/10/313 天前
3.5.5102026/10/303 天前
3.5.5002026/10/304 天前
3.5.4902026/10/304 天前

关于 @meshmakers/shared-auth 的常见问题

@meshmakers/shared-auth 是什么?

OAuth2/OIDC认证库,用于Angular应用 angular-oauth2-oidc.

如何安装 @meshmakers/shared-auth?

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

@meshmakers/shared-auth 支持 TypeScript 吗?

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

@meshmakers/shared-auth 支持 ESM 还是 CommonJS?

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

@meshmakers/shared-auth 可以商用吗?

@meshmakers/shared-auth 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@meshmakers/shared-auth 还在维护吗?

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