Add Login Using the Single-Page Login Flow

This tutorial will help you add login to your single-page application (SPA) using the single-page login flow. If you want to learn how the flow works and why you should use it, see Single-Page Login Flow. If you want to learn to call your API from a SPA, see Call Your API Using the Single-Page Login Flow.

Auth0 makes it easy to implement the single-page login flow by using:

  • Auth0.js: The easiest way to implement the single-page login flow, which will do most of the heavy-lifting for you. Our Single-Page App Quickstarts will walk you through the process.
  • Authentication API: If you prefer to roll your own solution, keep reading to learn how to call our API directly.

Following successful login, your application will have access to the user's ID Token and Access Token, as well as an authorization code that can be exchanged with Auth0 for an additional Access Token. The ID Token will contain basic user profile information, and the Access Token can be used to call the Auth0 /userinfo endpoint or your own protected APIs.

Prerequisites

Before beginning this tutorial:

Steps

  1. Authorize the user: Request the user's authorization and redirect back to your app.
  2. Request tokens: If the SPA has a back-end, exchange your authorization code for tokens, including a secure Access Token.

Optional: Explore Sample Use Cases

Authorize the user

To begin the flow, you'll need to get the user's authorization. This step may include one or more of the following processes:

  • Authenticating the user;
  • Redirecting the user to an Identity Provider to handle authentication;
  • Checking for active SSO sessions;
  • Obtaining user consent for the requested permission level, unless consent has been previously given.

To authorize the user, your app must send the user to the authorization URL.

Example authorization URL

https://YOUR_AUTH0_DOMAIN/authorize?
    response_type=YOUR_RESPONSE_TYPE&
    client_id=YOUR_CLIENT_ID&
    redirect_uri=https://YOUR_APP/callback&
    state=STATE&
    nonce=NONCE

Parameters

Parameter Name Description
response_type Denotes the kind of credential that Auth0 will return (code or token). For the Implicit Flow, the value can be id_token, token, or id_token token. Specifically, id_token returns an ID Token, and token returns an Access Token.
client_id Your application's Client ID. You can find this value at your Application's Settings.
redirect_uri The URL to which Auth0 will redirect the browser after authorization has been granted by the user. You must specify this URL as a valid callback URL in your Application Settings.

Warning: Per the OAuth 2.0 Specification, Auth0 removes everything after the hash and does not honor any fragments.
scope Specifies the scopes for which you want to request authorization, which dictate which claims (or user attributes) you want returned. These must be separated by a space. You can request any of the standard OIDC scopes about users, such as profile and email, custom claims conforming to a namespaced format, or any scopes supported by the target API (for example, read:contacts).
state (recommended) An opaque arbitrary alphanumeric string that your app adds to the initial request and Auth0 includes when redirecting back to your application. To see how to use this value to prevent cross-site request forgery (CSRF) attacks, see Use the State Parameter Against CSRF Attacks.
nonce (required for response_type containing id_token token, otherwise recommended) A cryptographically random string that your app adds to the initial request and Auth0 includes inside the ID Token, used to prevent token replay attacks.
connection (optional) Forces the user to sign in with a specific connection. For example, you can pass a value of github to send the user directly to GitHub to log in with their GitHub account. When not specified, the user sees the Auth0 Lock screen with all configured connections. You can see a list of your configured connections on the Connections tab of your application.

As an example, your HTML snippet for your authorization URL when adding login to your app might look like:

<a href="https://YOUR_AUTH0_DOMAIN/authorize?
  response_type=id_token token&
  client_id=YOUR_CLIENT_ID&
  redirect_uri=https://YOUR_APP/callback&
  scope=read:tests&
  state=xyzABC123&
  nonce=eq...hPmz">
  Sign In
</a>

Response

If all goes well, you'll receive an HTTP 302 response. The requested credentials are included in a hash fragment at the end of the URL:

HTTP/1.1 302 Found
Location: https://YOUR_APP/callback#access_token=ey...MhPw&expires_in=7200&token_type=Bearer&id_token=ey...Fyqk&state=xyzABC123

Note that the returned values depend on what you requested as a response_type.

Response Type Components
id_token ID Token
token Access Token (plus expires_in and token_type values)
id_token token ID Token, Access Token (plus expires_in and token_type values)

Auth0 will also return any state value you included in your call to the authorization URL.

You should validate your tokens before saving them. To learn how, see Validate an ID Token and Verify Access Tokens.

ID Tokens contain user information that must be decoded and extracted.

Access Tokens are used to call the Auth0 Authentication API's /userinfo endpoint or another API. If you are calling your own API, the first thing your API will need to do is verify the Access Token.

Sample Use Cases

Basic Authentication Request

This example shows the most basic request you can make when authorizing the user in step 1. It displays the Auth0 login screen and allows the user to sign in with any of your configured connections:

https://YOUR_AUTH0_DOMAIN/authorize?
    response_type=id_token&
    client_id=YOUR_CLIENT_ID&
    redirect_uri=https://YOUR_APP/callback&
    nonce=NONCE

This will return an ID Token, which you can parse from your redirect URL.

Request the User's Name and Profile Picture

In addition to the usual user authentication, this example shows how to request additional user details, such as name and picture.

To request the user's name and picture, you need to add the appropriate scopes when authorizing the user in step 1:

https://YOUR_AUTH0_DOMAIN/authorize?
    response_type=id_token token&
    client_id=YOUR_CLIENT_ID&
    redirect_uri=https://YOUR_APP/callback&
    scope=openid%20name%20picture&
    state=STATE&
    nonce=NONCE

Now, your ID Token will contain the requested name and picture claims. When you decode the ID Token, it will look similar to:

{
  "name": "jerrie@...",
  "picture": "https://s.gravatar.com/avatar/6222081fd7dcea7dfb193788d138c457?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fje.png",
  "iss": "https://auth0pnp.auth0.com/",
  "sub": "auth0|581...",
  "aud": "xvt...",
  "exp": 1478113129,
  "iat": 1478077129
}

Request a User Log In with GitHub

In addition to the usual user authentication, this example shows how to send users directly to a social identity provider, such as GitHub. For this example to work, you will first need to configure the appropriate connection in the Auth0 Dashboard and get the connection name from the Settings tab.

To send users directly to the GitHub login screen, you need to pass the connection parameter and set its value to the connection name (in this case, github) when authorizing the user in step 1:

https://YOUR_AUTH0_DOMAIN/authorize?
    response_type=id_token token&
    client_id=YOUR_CLIENT_ID&
    redirect_uri=https://YOUR_APP/callback&
    scope=openid%20name%20picture&
    state=STATE&
    nonce=NONCE&
    connection=github

Now, your ID Token will contain a sub claim with the user's unique ID returned from GitHub. When you decode the ID Token, it will look similar to:

{
  "name": "Jerrie Pelser",
  "nickname": "jerriep",
  "picture": "https://avatars.githubusercontent.com/u/1006420?v=3",
  "iss": "https://auth0pnp.auth0.com/",
  "sub": "github|100...",
  "aud": "xvt...",
  "exp": 1478114742,
  "iat": 1478078742
}

For a list of possible connections, see Identity Providers Supported by Auth0.

Keep Reading