Skip to content
Auth0 authentication for Single Page Applications (SPA) with PKCE
TypeScript JavaScript HTML
Branch: master
Clone or download
lbalmaceda Merge pull request #355 from auth0/fix/readme
Fixed CDN version in readme.md
Latest commit 7e81147 Feb 13, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Configure CI build to store screenshots from integration tests (#349) Feb 5, 2020
.github Setup the .github/stale.yml for Probot:Stale Oct 26, 2019
.vscode Initial Commit Apr 10, 2019
__mocks__ Add 'lock' to prevent `getTokenSilently` to be invoked in parallel (#238 Oct 23, 2019
__tests__ Configurable timeout for getTokenSilently() (#347) Feb 10, 2020
cypress [SDK-1308] Return appState value on error from handleRedirectCallback ( Feb 4, 2020
docs Release v1.6.4 (#353) Feb 11, 2020
scripts Enchancements to the build process - changelog generation (#332) Jan 15, 2020
src Release v1.6.4 (#353) Feb 11, 2020
static [SDK-1308] Return appState value on error from handleRedirectCallback ( Feb 4, 2020
.eslintrc.security Add security linters May 6, 2019
.gitignore Enchancements to the build process - changelog generation (#332) Jan 15, 2020
.npmignore Initial Commit Apr 10, 2019
.prettierignore Initial Commit Apr 10, 2019
.prettierrc Initial Commit Apr 10, 2019
CHANGELOG.md Release v1.6.4 (#353) Feb 11, 2020
CONTRIBUTING.md Initial Commit Apr 10, 2019
FAQ.md Fix safari10 initialization error (#232) Oct 10, 2019
Jenkinsfile only publish on master Apr 10, 2019
LICENSE Initial Commit Apr 10, 2019
README.md Fixed CDN version Feb 13, 2020
cypress.json Initial Commit Apr 10, 2019
jest.config.js Initial Commit Apr 10, 2019
package-lock.json updates browser-tabs-lock to fix issue of long acquired lock. Fixes #278 Nov 13, 2019
package.json Release v1.6.4 (#353) Feb 11, 2020
rollup.config.js Add IE11 polyfills (#154) Aug 23, 2019
security-tslint.json Add security linters May 6, 2019
tsconfig.json Add IE11 polyfills (#154) Aug 23, 2019
typedoc.js Add IE11 polyfills (#154) Aug 23, 2019

README.md

@auth0/auth0-spa-js

Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE.

CircleCI License

Table of Contents

Documentation

Installation

From the CDN:

<script src="https://cdn.auth0.com/js/auth0-spa-js/1.6/auth0-spa-js.production.js"></script>

Using npm:

npm install @auth0/auth0-spa-js

Using yarn:

yarn add @auth0/auth0-spa-js

Getting Started

Creating the client

Create an Auth0Client instance before rendering or initializing your application. You should only have one instance of the client.

import createAuth0Client from '@auth0/auth0-spa-js';

//with async/await
const auth0 = await createAuth0Client({
  domain: '<AUTH0_DOMAIN>',
  client_id: '<AUTH0_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>'
});

//with promises
createAuth0Client({
  domain: '<AUTH0_DOMAIN>',
  client_id: '<AUTH0_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>'
}).then(auth0 => {
  //...
});

1 - Login

<button id="login">Click to Login</button>
//with async/await

//redirect to the Universal Login Page
document.getElementById('login').addEventListener('click', async () => {
  await auth0.loginWithRedirect();
});

//in your callback route (<MY_CALLBACK_URL>)
window.addEventListener('load', async () => {
  const redirectResult = await auth0.handleRedirectCallback();
  //logged in. you can get the user profile like this:
  const user = await auth0.getUser();
  console.log(user);
});

//with promises

//redirect to the Universal Login Page
document.getElementById('login').addEventListener('click', () => {
  auth0.loginWithRedirect().catch(() => {
    //error while redirecting the user
  });
});

//in your callback route (<MY_CALLBACK_URL>)
window.addEventListener('load', () => {
  auth0.handleRedirectCallback().then(redirectResult => {
    //logged in. you can get the user profile like this:
    auth0.getUser().then(user => {
      console.log(user);
    });
  });
});

2 - Calling an API

<button id="call-api">Call an API</button>
//with async/await
document.getElementById('call-api').addEventListener('click', async () => {
  const accessToken = await auth0.getTokenSilently();
  const result = await fetch('https://myapi.com', {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${accessToken}`
    }
  });
  const data = await result.json();
  console.log(data);
});

//with promises
document.getElementById('call-api').addEventListener('click', () => {
  auth0
    .getTokenSilently()
    .then(accessToken =>
      fetch('https://myapi.com', {
        method: 'GET',
        headers: {
          Authorization: `Bearer ${accessToken}`
        }
      })
    )
    .then(result => result.json())
    .then(data => {
      console.log(data);
    });
});

3 - Logout

<button id="logout">Logout</button>
import createAuth0Client from '@auth0/auth0-spa-js';

document.getElementById('logout').addEventListener('click', () => {
  auth0.logout();
});

Contributing

We appreciate feedback and contribution to this repo! Before you get started, please see the following:

Support + Feedback

This SDK is in Early Access with selected stakeholders.

We process feedback and provide support via private channels.

Frequently Asked Questions

For a rundown of common issues you might encounter when using the SDK, please check out the FAQ.

Vulnerability Reporting

Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

What is Auth0?

Auth0 helps you to easily:

  • implement authentication with multiple identity providers, including social (e.g., Google, Facebook, Microsoft, LinkedIn, GitHub, Twitter, etc), or enterprise (e.g., Windows Azure AD, Google Apps, Active Directory, ADFS, SAML, etc.)
  • log in users with username/password databases, passwordless, or multi-factor authentication
  • link multiple user accounts together
  • generate signed JSON Web Tokens to authorize your API calls and flow the user identity securely
  • access demographics and analytics detailing how, when, and where users are logging in
  • enrich user profiles from other data sources using customizable JavaScript rules

Why Auth0?

License

This project is licensed under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.