Loading...
Loading...
Use when adding authentication to Angular applications with route guards and HTTP interceptors - integrates @auth0/auth0-angular SDK for SPAs
npx skill4agent add auth0/agent-skills auth0-angularauth0-quickstartauth0-react-nativenpm install @auth0/auth0-angularsrc/environments/environment.tsexport const environment = {
production: false,
auth0: {
domain: 'your-tenant.auth0.com',
clientId: 'your-client-id',
authorizationParams: {
redirect_uri: window.location.origin
}
}
};src/app/app.config.tsimport { 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
})
]
};src/app/app.module.tsimport { 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 {}src/app/app.component.tsimport { 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 } });
}
}ng serve| Mistake | Fix |
|---|---|
| Forgot to add redirect URI in Auth0 Dashboard | Add your application URL (e.g., |
| Not configuring AuthModule properly | Must call |
| Accessing auth before initialization | Use |
| Storing tokens manually | Never manually store tokens - SDK handles secure storage automatically |
| Missing HTTP interceptor | Use |
| Route guard not protecting routes | Apply |
auth0-quickstartauth0-migrationauth0-mfaAuthServiceisAuthenticated$user$loginWithRedirect()logout()getAccessTokenSilently()