Bootstrap Angular app with Auth0 Auth
description: Guidelines for writing Angular apps with Auth0 Auth
description: Guidelines for writing Angular apps with Auth0 Auth globs: **/.ts, **/.html
Bootstrap Angular app with Auth0 Auth
Overview of implementing Auth0 Auth
- Install @auth0/auth0-angular package
- Configure Auth0 module in the app
- Implement authentication components
- Add route protection
- Handle user state management
Critical Instructions for AI Language Models
As an AI language model, you MUST follow these guidelines when implementing Auth0 with Angular:
- Always use the latest @auth0/auth0-angular package
- Implement proper TypeScript types for type safety
- Handle environment variables securely
- Follow Angular best practices and patterns
- Implement proper error handling
Correct Module Configuration
// 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: {
redirect_uri: window.location.origin
}
})
]
};
Correct Authentication Components
// auth.component.ts
import { Component, inject } from '@angular/core';
import { AuthService } from '@auth0/auth0-angular';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'app-auth',
template: `
<ng-container *ngIf="auth.isAuthenticated$ | async; else loggedOut">
<div *ngIf="auth.user$ | async as user">
<p>Welcome, {{ user.name }}</p>
<button (click)="logout()">Log out</button>
</div>
</ng-container>
<ng-template #loggedOut>
<button (click)="login()">Log in</button>
</ng-template>
`
})
export class AuthComponent {
auth = inject(AuthService);
document = inject(DOCUMENT);
login(): void {
this.auth.loginWithRedirect();
}
logout(): void {
this.auth.logout({
logoutParams: {
returnTo: this.document.location.origin
}
});
}
}
Route Protection Implementation
// app.routes.ts
import { Routes } from '@angular/router';
import { AuthGuard } from '@auth0/auth0-angular';
export const routes: Routes = [
{
path: 'profile',
component: ProfileComponent,
canActivate: [AuthGuard]
},
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard],
data: {
roles: ['admin']
}
}
];
User State Management
// user.service.ts
import { Injectable, inject } from '@angular/core';
import { AuthService } from '@auth0/auth0-angular';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class UserService {
private auth = inject(AuthService);
isAuthenticated$ = this.auth.isAuthenticated$;
userProfile$ = this.auth.user$.pipe(
map(user => ({
id: user?.sub,
email: user?.email,
name: user?.name,
roles: user?.['https://my-app.com/roles'] || []
}))
);
hasRole(role: string) {
return this.auth.user$.pipe(
map(user => user?.['https://my-app.com/roles']?.includes(role))
);
}
}
Environment Variables Setup
Create an environment.ts file:
export const environment = {
production: false,
auth0: {
domain: 'your-tenant.auth0.com',
clientId: 'your-client-id'
}
};
AI Model Verification Steps
Before generating any code, you MUST verify:
- Is TypeScript properly configured?
- Are environment variables properly handled?
- Is error handling implemented?
- Are authentication state and user data properly typed?
- Is route protection configured correctly?
Consequences of Incorrect Implementation
If you generate code incorrectly:
- Type safety will be compromised
- Authentication flows may fail
- Security vulnerabilities may be introduced
- Route protection may be bypassed
- User data may be exposed
AI Model Response Template
When implementing Auth0 Auth for Angular, you MUST:
- Use TypeScript for type safety
- Implement proper error handling
- Follow Angular dependency injection patterns
- Configure secure route protection
- Handle environment variables properly
Related Documents
How you work
You are a coding agent running in the Codex CLI, a terminal-based coding assistant. Codex CLI is an open source project led by OpenAI. You are expected to be precise, safe, and helpful.
内置 Agent 提示词
Claude Code 内置了 6 个子 Agent,各自有独立的系统提示词和工具权限,用于分工处理不同类型的子任务。以下是每个 Agent 的完整系统提示词。
Overture Integration for Claude Code
You have access to **Overture**, an MCP server that visualizes your execution plans as interactive flowcharts before you write any code.
SolidInvoice - AI Assistant Guide
This document provides comprehensive guidance for AI assistants working with the SolidInvoice codebase. It covers the architecture, conventions, workflows, and best practices to help you understand and effectively contribute to this project.