claude-skills/

Anthropic公式スキル・プラグインの日本語ディレクトリ

last sync 22h ago
スキルOfficialsecurity

🔐auth0-angular

プラグイン
auth0
ライセンス
Apache-2.0

説明

次のような場合に使用: Auth0 のログイン、ログアウト、ルートガード、または HTTP インターセプターを Angular アプリケーションに追加する場合。 SPA 向けの `@auth0/auth0-angular` を統合する。 ユーザーが「Angular のルートを保護したい」や「Angular に認証を追加したい」と言った際に、SDK 名を明示していなくても使用すること。

原文を表示

Use when adding Auth0 login, logout, route guards, or HTTP interceptors to an Angular application. Integrates @auth0/auth0-angular for SPAs — use even if the user says "protect my Angular routes" or "add authentication to Angular" without naming the SDK.

ユースケース

  • Angular アプリにログイン機能を追加するとき
  • Angular アプリにログアウト機能を追加するとき
  • Angular のルートを保護したいとき
  • HTTP インターセプターを設定するとき

本文(日本語訳)

Auth0 Angular インテグレーション

@auth0/auth0-angular を使用して、Angular アプリケーションに認証機能を追加します。


前提条件

  • Angular 13 以降のアプリケーション
  • Auth0 アカウントおよびアプリケーションの設定済み環境
  • Auth0 をまだ設定していない場合は、先に auth0-quickstart スキルを使用してください

使用しない場合

  • AngularJS (1.x) — この SDK は Angular 13 以降が必要です。AngularJS にはレガシーソリューションを使用してください
  • モバイルアプリケーション — React Native の場合は auth0-react-native、Ionic の場合はネイティブ SDK を使用してください
  • バックエンド API — サーバーサイドの言語に対応した JWT 検証ミドルウェアを使用してください

クイックスタート手順

1. SDK のインストール

npm install @auth0/auth0-angular

2. 環境設定

Auth0 CLI を使った自動セットアップの場合、完全なスクリプトは セットアップガイド を参照してください。

手動セットアップの場合:

src/environments/environment.ts を更新します:

export const environment = {
  production: false,
  auth0: {
    domain: 'your-tenant.auth0.com',
    clientId: 'your-client-id',
    authorizationParams: {
      redirect_uri: window.location.origin
    }
  }
};

3. Auth モジュールの設定

スタンドアロンコンポーネントの場合 (Angular 14 以降):

src/app/app.config.ts を更新します:

import { ApplicationConfig } from '@angular/core';
import { provideAuth0 } from '@auth0/auth0-angular';
import { environment } from '../environments/environment';

export const appConfig: ApplicationConfig = {
  providers: [
    provideAuth0({
      domain: environment.auth0.domain,
      clientId: environment.auth0.clientId,
      authorizationParams: environment.auth0.authorizationParams
    })
  ]
};

NgModule ベースのアプリの場合:

src/app/app.module.ts を更新します:

import { AuthModule } from '@auth0/auth0-angular';
import { environment } from '../environments/environment';

@NgModule({
  imports: [
    AuthModule.forRoot({
      domain: environment.auth0.domain,
      clientId: environment.auth0.clientId,
      authorizationParams: environment.auth0.authorizationParams
    })
  ]
})
export class AppModule {}

4. 認証 UI の追加

src/app/app.component.ts を更新します:

import { Component } from '@angular/core';
import { AuthService } from '@auth0/auth0-angular';

@Component({
  selector: 'app-root',
  template: `
    <div *ngIf="auth.isLoading$ | async; else loaded">
      <p>Loading...</p>
    </div>

    <ng-template #loaded>
      <ng-container *ngIf="auth.isAuthenticated$ | async; else loggedOut">
        <div *ngIf="auth.user$ | async as user">
          <img [src]="user.picture" [alt]="user.name" />
          <h2>Welcome, {{ user.name }}!</h2>
          <button (click)="logout()">Logout</button>
        </div>
      </ng-container>

      <ng-template #loggedOut">
        <button (click)="login()">Login</button>
      </ng-template>
    </ng-template>
  `
})
export class AppComponent {
  constructor(public auth: AuthService) {}

  login(): void {
    this.auth.loginWithRedirect();
  }

  logout(): void {
    this.auth.logout({ logoutParams: { returnTo: window.location.origin } });
  }
}

5. 認証のテスト

開発サーバーを起動して、ログインフローをテストします:

ng serve

詳細ドキュメント


よくある間違い

間違い 対処法
Auth0 ダッシュボードにリダイレクト URI を追加し忘れた Auth0 ダッシュボードの「Allowed Callback URLs」に、アプリケーションの URL(例: http://localhost:4200https://app.example.com)を追加してください
AuthModule が正しく設定されていない NgModule では AuthModule.forRoot()、スタンドアロン構成では provideAuth0() を必ず呼び出してください
初期化前に auth にアクセスしている isLoading$ Observable を使用して、SDK の初期化完了を待機してください
トークンを手動で保存している トークンを手動で保存しないでください。SDK が安全なストレージを自動的に管理します
API にトークンが送信されない トークンの自動付与には authHttpInterceptorFn、手動制御には getAccessTokenSilently() を使用してください。詳細は インテグレーションガイド を参照
ルートガードがルートを保護していない ルーティング設定の保護対象ルートに AuthGuard(または authGuardFn)を適用してください

関連スキル

  • auth0-quickstart — Auth0 の基本セットアップ
  • auth0-migration — 他の認証プロバイダーからの移行
  • auth0-mfa — 多要素認証 (MFA) の追加
  • auth0-cli — ターミナルからの Auth0 リソース管理

クイックリファレンス

主要サービス:

  • AuthService — メインの認証サービス
  • isAuthenticated$ — ユーザーのログイン状態を確認する Observable
  • user$ — ユーザープロファイル情報の Observable
  • loginWithRedirect() — ログインの開始
  • logout() — ユーザーのログアウト
  • getAccessTokenSilently() — アクセストークンの手動取得(HTTP インターセプターの代替手段)

よくあるユースケース:


参考資料

原文(English)を表示

Auth0 Angular Integration

Add authentication to Angular applications using @auth0/auth0-angular.


Prerequisites

  • Angular 13+ application
  • Auth0 account and application configured
  • If you don't have Auth0 set up yet, use the auth0-quickstart skill first

When NOT to Use

  • AngularJS (1.x) - This SDK requires Angular 13+, use legacy solutions for AngularJS
  • Mobile applications - Use auth0-react-native for React Native or native SDKs for Ionic
  • Backend APIs - Use JWT validation middleware for your server language

Quick Start Workflow

1. Install SDK

npm install @auth0/auth0-angular

2. Configure Environment

For automated setup with Auth0 CLI, see Setup Guide for complete scripts.

For manual setup:

Update src/environments/environment.ts:

export const environment = {
  production: false,
  auth0: {
    domain: 'your-tenant.auth0.com',
    clientId: 'your-client-id',
    authorizationParams: {
      redirect_uri: window.location.origin
    }
  }
};

3. Configure Auth Module

For standalone components (Angular 14+):

Update src/app/app.config.ts:

import { ApplicationConfig } from '@angular/core';
import { provideAuth0 } from '@auth0/auth0-angular';
import { environment } from '../environments/environment';

export const appConfig: ApplicationConfig = {
  providers: [
    provideAuth0({
      domain: environment.auth0.domain,
      clientId: environment.auth0.clientId,
      authorizationParams: environment.auth0.authorizationParams
    })
  ]
};

For NgModule-based apps:

Update src/app/app.module.ts:

import { AuthModule } from '@auth0/auth0-angular';
import { environment } from '../environments/environment';

@NgModule({
  imports: [
    AuthModule.forRoot({
      domain: environment.auth0.domain,
      clientId: environment.auth0.clientId,
      authorizationParams: environment.auth0.authorizationParams
    })
  ]
})
export class AppModule {}

4. Add Authentication UI

Update src/app/app.component.ts:

import { Component } from '@angular/core';
import { AuthService } from '@auth0/auth0-angular';

@Component({
  selector: 'app-root',
  template: `
    <div *ngIf="auth.isLoading$ | async; else loaded">
      <p>Loading...</p>
    </div>

    <ng-template #loaded>
      <ng-container *ngIf="auth.isAuthenticated$ | async; else loggedOut">
        <div *ngIf="auth.user$ | async as user">
          <img [src]="user.picture" [alt]="user.name" />
          <h2>Welcome, {{ user.name }}!</h2>
          <button (click)="logout()">Logout</button>
        </div>
      </ng-container>

      <ng-template #loggedOut">
        <button (click)="login()">Login</button>
      </ng-template>
    </ng-template>
  `
})
export class AppComponent {
  constructor(public auth: AuthService) {}

  login(): void {
    this.auth.loginWithRedirect();
  }

  logout(): void {
    this.auth.logout({ logoutParams: { returnTo: window.location.origin } });
  }
}

5. Test Authentication

Start your dev server and test the login flow:

ng serve

Detailed Documentation

  • Setup Guide - Automated setup scripts (Bash/PowerShell), CLI commands, manual configuration
  • Integration Guide - Protected routes with guards, HTTP interceptors, error handling
  • API Reference - Complete SDK API, configuration options, services reference, testing strategies

Common Mistakes

Mistake Fix
Forgot to add redirect URI in Auth0 Dashboard Add your application URL (e.g., http://localhost:4200, https://app.example.com) to Allowed Callback URLs in Auth0 Dashboard
Not configuring AuthModule properly Must call AuthModule.forRoot() in NgModule or provideAuth0() in standalone config
Accessing auth before initialization Use isLoading$ observable to wait for SDK initialization
Storing tokens manually Never manually store tokens - SDK handles secure storage automatically
No token sent to API Use either authHttpInterceptorFn for automatic token attachment, or getAccessTokenSilently() for manual control — see Integration Guide
Route guard not protecting routes Apply AuthGuard (or authGuardFn) to protected routes in routing config

Related Skills

  • auth0-quickstart - Basic Auth0 setup
  • auth0-migration - Migrate from another auth provider
  • auth0-mfa - Add Multi-Factor Authentication
  • auth0-cli - Manage Auth0 resources from the terminal

Quick Reference

Core Services:

  • AuthService - Main authentication service
  • isAuthenticated$ - Observable check if user is logged in
  • user$ - Observable user profile information
  • loginWithRedirect() - Initiate login
  • logout() - Log out user
  • getAccessTokenSilently() - Get access token manually (alternative to HTTP interceptor)

Common Use Cases:


References

原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。