Docs

AngularJS: Login

View on Github

AngularJS: Login

Gravatar for luis.rudge@auth0.com
By Luis Rudge

This tutorial demonstrates how to add user login to an Angular.js application using Auth0. We recommend you to Log in to follow this quickstart with examples configured for your account.

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: AngularJS 1.6+

New to Auth? Learn How Auth0 works, how it integrates with Single-Page Applications and which protocol it uses.

Access Token Structure

Configure Auth0

Opaque Access Tokens

Get Your Application Keys

When you signed up for Auth0, a new application was created for you, or you could have created a new one.

You will need some details about that application to communicate with Auth0. You can get these details from the Application Settings section in the Auth0 dashboard.

You need the following information:

  • Domain
  • Client ID

If you download the sample from the top of this page these details are filled out for you.

If you have more than one application in your account, the sample comes with the values for your Default App.

App Dashboard

JSON Web Token Access Tokens

Configure Callback URLs

A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated.

The callback URL for your app must be whitelisted in the Allowed Callback URLs field in your Application Settings. If this field is not set, users will be unable to log in to the application and will get an error.

If you are following along with the sample project you downloaded from the top of this page, you should set the Allowed Callback URL to http://localhost:3000/callback.

Access Token Security

Configure Logout URLs

A logout URL is a URL in your application that Auth0 can return to after the user has been logged out of the authorization server. This is specified in the returnTo query parameter.

The logout URL for your app must be whitelisted in the Allowed Logout URLs field in your Application Settings. If this field is not set, users will be unable to log out from the application and will get an error.

If you are following along with the sample project you downloaded from the top of this page, the logout URL you need to whitelist in the Allowed Logout URLs field is http://localhost:3000.

Access Token Lifetime

Configure Allowed Web Origins

You need to whitelist the URL for your app in the Allowed Web Origins field in your Application Settings. If you don't whitelist your application URL, the application will be unable to automatically refresh the authentication tokens and your users will be logged out the next time they visit the application, or refresh the page.

Custom APIs

Integrate Auth0 in your Application

/userinfo endpoint

Loading auth0.js

You need the auth0.js library to integrate Auth0 into your application. You can either install the library locally in your application or load it from CDN.

Next Steps

Loading via dependencies

Install auth0.js using npm or yarn.

Once you install auth0.js, add it to your build system or bring it in to your project with a script tag.

Loading it from CDN

If you do not want to use a package manager, you can retrieve auth0.js from Auth0's CDN.

Install angular-auth0

To use auth0.js in your AngularJS application, you need a thin wrapper called angular-auth0.

Install angular-auth0 using npm or yarn.

Once you install angular-auth0, add it to your build system or bring it in to your project as a script tag.

Authentication with Auth0

Universal Login is the easiest way to set up authentication in your application. We recommend using it for the best experience, best security and the fullest array of features. This guide will use it to provide a way for your users to log in to your AngularJS application.

You can also embed the login dialog directly in your application using the Lock widget. If you use this method, some features, such as single sign-on, will not be accessible.

When a user logs in, Auth0 returns three items:

You can use these items in your application to set up and manage authentication.

Configure angular-auth0

The angular-auth0 wrapper comes with a provider called angularAuth0Provider. The provider has an init method which takes a configuration object used to create an instance of the WebAuth object from auth0.js.

Inject the angularAuth0Provider provider. In the options object you pass to angularAuth0Provider.init, include the following information:

  • Configuration for your application and domain
  • Response type, to show that you need a user's Access Token and an ID Token after authentication
  • Audience and scope, specifying that you need an access_token that can be used to invoke the /userinfo endpoint.
  • The URL where you want to redirect your users after authentication.

In this tutorial, the route is /callback, which is implemented in the Add a Callback Component step.

Create an Authentication Service

Create a reusable service to manage and coordinate user authentication. You can call the service's methods from your application.

You can give the service any name. In the examples below, the service is authService and the filename is auth.service.js.

Create a service and provide a login method that calls the authorize method from angular-auth0.

Checkpoint: Try to call the login method from somewhere in your application to see the login page.

hosted login

Handle Authentication Tokens

Add more methods to the authService service to handle authentication in the app.

The example below shows the following methods:

  • handleAuthentication: looks for the result of authentication in the URL hash. Then, the result is processed with the parseHash method from auth0.js.
  • localLogin: sets the user's Access Token and ID Token, and the Access Token's expiry time.
  • renewSession: performs silent authentication to renew the session.
  • logout: removes the user's tokens and expiry time from memory.
  • isAuthenticated: checks whether the expiry time for the user's Access Token has passed.

Provide a Login Control

Provide a component with controls for the user to log in and log out.

This example uses Bootstrap styles. You can use any style library, or not use one at all.

Depending on whether the user is authenticated or not, they see the Log Out or Log In button. The ng-click events on the buttons make calls to the authService service to let the user log in or out. When the user clicks Log In, they are redirected to the login page.

The login page uses the Lock widget. To learn more about Universal Login and the login page, see the Universal Login documentation. To customize the look and feel of the Lock widget, see the Lock customization options documentation.

Add a Callback Component

When you use the login page, your users are taken away from your application. After they authenticate, they are automatically returned to your application and a client-side session is set for them.

This example assumes you are using path-based routing by setting $locationProvider.html5Mode(true). If you are using hash-based routing, you will not be able to specify a dedicated callback route. The URL hash will be used to hold the user's authentication information.

You can select any URL in your application for your users to return to. We recommend creating a dedicated callback route. If you create a single callback route:

  • You don't have to whitelist many, sometimes unknown, callback URLs.
  • You can display a loading indicator while the application sets up a client-side session.

Create a controller and a template to use for a callback route. Add a loading indicator.

To display a loading indicator, you need a loading spinner or another indicator in the assests directory. See the downloadable sample for demonstration.

After authentication, your users are taken to the /callback route. They see the loading indicator while the application sets up a client-side session for them. After the session is set up, the users are redirected to the /home route.

Process the Authentication Result

When a user authenticates at the login page, they are redirected to your application. Their URL contains a hash fragment with their authentication information. The handleAuthentication method in the authService service processes the hash.

Call the handleAuthentication method in your app's run block. The method processes the authentication hash while your app loads.

Use Auth0 for FREE