Bootstrap Angular app with Auth0 Auth
Guides AI models to generate Angular apps with Auth0 authentication, covering setup, components, route protection, and user state.
What this file does
Guides AI models to generate Angular apps with Auth0 authentication, covering setup, components, route protection, and user state.
When to use it
- Starting a new Angular project that needs Auth0 login
- Adding Auth0 authentication to an existing Angular app
- Teaching an AI model how to implement Auth0 in Angular
- Standardizing Auth0 integration across multiple Angular projects
Assumes this stack
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
What's inside
7 sections, 5 code examples, 1 environment template, 1 verification checklist
Change this for your project
- Replace
'your-tenant.auth0.com'with your Auth0 domain - Replace
'your-client-id'with your Auth0 client ID - Replace
'https://my-app.com/roles'with your custom claims namespace
Where it goes
Keep it in your repository where the agent or team that needs it will read it.
Worth borrowing
- Injecting AuthService directly in components for login/logout
- Using AuthGuard for route protection with optional role-based data
- Mapping user claims into a typed profile object in a service
Related Documents
How you work
Defines personality, planning, task execution, and communication conventions for a coding agent in the Codex CLI environment.
How you work
Defines personality, planning, task execution, and communication guidelines for a coding agent in Codex CLI.
内置 Agent 提示词
Documents the system prompts, tool permissions, and model assignments for six built-in subagents in Claude Code.
perplexity-ai_20250112
Defines the system prompt for Perplexity AI, instructing the assistant on how to answer queries using search results with strict formatting, citation, and query-type rules.