Authentication using the Auth0 to an Angular application

Rodrigo Kamada
4 min readNov 16, 2021

--

Introduction

Angular is a development platform for building WEB, mobile and desktop applications using HTML, CSS and TypeScript (JavaScript). Currently, Angular is at version 14 and Google is the main maintainer of the project.

Auth0 is an easy to implement, adaptable authentication and authorization platform.

Prerequisites

Before you start, you need to install and configure the tools:

Getting started

Create and configure the account on the Auth0

1. Let’s create the account. Access the site https://auth0.com/ and click on the button Sign up.

Auth0 - Home page

2. Fill in the field Email and click on the button SIGN UP.

Auth0 - Sign up

3. Fill in the fields Email, Password and click on the button Continue.

Auth0 - Create your account

4. Click on the option Personal and click on the button NEXT.

Auth0 - Account type

5. Let’s create the application. Click on the menu Applications and click on the menu Applications.

Auth0 - Getting started

6. Click on the link with the application name.

Auth0 - Applications

7. Copy the domain that has been generated, in my case, the domain dev-5tf99p7c.us.auth0.com was copied, copy the Client ID that has been generated, in my case, the Client ID GBPB42qhMWCtvrwGmYxvm5cbHXU68nzG was copied, select the option Single Page Application in the field Application Type, fill in the fields Allowed Callback URLs, Allowed Logout URLs, Allowed Web Origins and click on the button Save Changes.

Auth0 - Applications settings

8. Check the registered email.

Auth0 - Verify your account

Create the Angular application

1. Let’s create the application with the Angular base structure using the @angular/cli with the route file and the SCSS style format.

2. Install and configure the Bootstrap CSS framework. Do steps 2 and 3 of the post Adding the Bootstrap CSS framework to an Angular application.

3. Configure the variable auth0.domain with the Auth0 domain and the variable auth0.clientId with the Auth0 Client ID in the src/environments/environment.ts and src/environments/environment.prod.ts files as below.

4. Install the @auth0/auth0-angular library.

5. Create the SignInComponent component.

6. Change the src/app/sign-in/sign-in.component.ts file. Import the Router service and create the signIn method as below.

7. Change the src/app/sign-in/sign-in.component.html file. Add the lines as below.

8. Create the ProfileComponent component.

9. Change the src/app/profile/profile.component.ts file. Import the AuthService service and add the lines as below.

10. Change the src/app/profile/profile.component.html file and add the lines as below.

11. Change the src/app/app.component.ts file. Import the AuthService service and create the signOut method as below.

12. Change the src/app/app.component.html file and add the menu as below.

13. Change the src/app/app-routing.module.ts file and add the routes as below.

14. Change the src/app/app.module.ts file. Import the FormsModule and AuthModule modules, the ProfileComponent and SignInComponent components and configure the AuthModule module as below.

15. Run the application with the command below.

16. Ready! Access the URL http://localhost:4200/ and check if the application is working. See the application working on GitHub Pages and Stackblitz.

Angular Auth0

Testing the application sign in

1. Let’s test the application sign in. Access the URL http://localhost:4200/ and click on the button Sign in.

Application - Sign in

2. Click on the button Sign up.

Application - Log in

3. Fill in the fields Email Address, Password and click on the button Continue.

Application - Sign up

4. Click on the button Accept.

Application - Authorize app

5. You will be redirected to the application.

Application - Profile

6. Check the registered email.

Application - Verify your account

7. Ready! We test the application sign in and profile view. Auth0 documentation is available at https://auth0.com/docs.

The application repository is available at https://github.com/rodrigokamada/angular-auth0.

This tutorial was posted on my blog in portuguese.

--

--

Rodrigo Kamada
Rodrigo Kamada

Written by Rodrigo Kamada

👨‍💻 Software Developer | ✍️ Technical Content Creator | 🤝 Open Source Contributor | 🎙️ Speaker | 👨‍🏫 Mentor | 🙌 Ambassador | ☁️ AWS Community Builder

Responses (1)