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 18.x and newer using the Angular CLI. For older versions of Angular, use the Auth0 Angular SDK v2.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
The CLI script has already created your environment file. Now configure the Auth0 module in your app:
src/main.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.Create Login, Logout and Profile Components
Create the component files manually for better controlAdd the following code to each component:Now update the main App Component and add styling:
- App Component
- Global Styles
Replace the contents of
src/app/app.component.ts: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 prefer using NgModules instead of standalone components, here’s how to configure the SDK:
src/app/app.module.ts
Protecting Routes with AuthGuard
Protecting Routes with AuthGuard
Use the modern functional guard to protect routes that require authentication:Then configure routing in your main.ts:
src/app/app.routes.ts
src/main.ts
Calling Protected APIs
Calling Protected APIs
Configure the HTTP interceptor to automatically attach tokens to API calls:
src/main.ts