Use AI to integrate Auth0
Use AI to integrate Auth0
If you use an AI coding assistant like Claude Code, Cursor, or GitHub Copilot, you can add Auth0 authentication automatically in minutes using agent skills.Install:Then ask your AI assistant:Your AI assistant will automatically create your Auth0 application, fetch credentials, install
@auth0/auth0-angular, create route guards and HTTP interceptors, and configure your environment. Full agent skills documentation →Prerequisites: Before you begin, ensure you have the following installed:Verify installation:
node --version && npm --versionAngular Version Compatibility: This quickstart works with Angular 19 through 21 using the Angular CLI. The @auth0/auth0-angular SDK supports Angular 13 and newer.Get Started
This quickstart demonstrates how to add Auth0 authentication to an Angular application. You’ll build a secure single-page app with login and logout functionality using Angular’s dependency injection system and the Auth0 Angular SDK.Setup your Auth0 App
Next up, you need to create a new app on your Auth0 tenant and add the environment variables to your project.You have three options to set up your Auth0 app: use the Quick Setup tool (recommended), run a CLI command, or configure manually via the Dashboard:
- Quick Setup (recommended)
- CLI
- Dashboard
Create an Auth0 App and copy the pre-filled
.env file with the right configuration values.Configure the Auth0 module
With your environment file in place from the previous step, add
provideAuth0 to the providers array in your app.config.ts:src/app/app.config.ts
If you set up your Auth0 app manually via the dashboard, create
src/environments/environment.ts with your domain and client ID from the dashboard.Angular 20+ projects also include provideBrowserGlobalErrorListeners() in this file — keep it in the array alongside provideAuth0. No changes to main.ts are needed.Create Login, Logout and Profile Components
Use the Angular CLI to scaffold the component files:Add the following code to each component:Now update the main App Component and add styling:
- App Component
- Global Styles
Replace the contents of your app component file (
src/app/app.ts on Angular 20+, or src/app/app.component.ts on Angular 19):src/app/app.ts
CheckpointYou should now have a fully functional Auth0 login page running on your localhost
Advanced Usage
Using Traditional NgModule Approach
Using Traditional NgModule Approach
If you created your project with
--standalone=false or prefer using NgModules instead of standalone components, here’s how to configure the SDK with Angular 20+ CLI-generated projects:src/main.ts
src/app/app-module.ts
If you’re using Angular 19, your generated files are typically
app.component.ts, app.module.ts, and app-routing.module.ts, and main.ts uses platformBrowserDynamic instead of platformBrowser.Angular 20+ generates app.ts, app-module.ts, and app-routing-module.ts (no dots in filenames), and main.ts uses platformBrowser as shown above.In all cases, NgModule projects bootstrap via bootstrapModule() rather than the bootstrapApplication() approach shown in the main quickstart.Protecting Routes with AuthGuard
Protecting Routes with AuthGuard
Use the modern functional guard to protect routes that require authentication:Then add
src/app/app.routes.ts
provideAuth0 and the router to your app.config.ts (if you haven’t already from Step 4):src/app/app.config.ts
Calling Protected APIs
Calling Protected APIs
Configure the HTTP interceptor to automatically attach tokens to API calls. Add
provideHttpClient with the Auth0 interceptor and an audience to your app.config.ts:src/app/app.config.ts