@auth0/auth0-spa-js
Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE.
![License](https://img.shields.io/:license-mit-blue.svg?style=flat)
Table of Contents
Documentation
Installation
From the CDN:
<script src="https://cdn.auth0.com/js/auth0-spa-js/1.7.0-beta.1/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';
const auth0 = await createAuth0Client({
domain: '<AUTH0_DOMAIN>',
client_id: '<AUTH0_CLIENT_ID>',
redirect_uri: '<MY_CALLBACK_URL>'
});
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>
document.getElementById('login').addEventListener('click', async () => {
await auth0.loginWithRedirect();
});
window.addEventListener('load', async () => {
const redirectResult = await auth0.handleRedirectCallback();
const user = await auth0.getUser();
console.log(user);
});
document.getElementById('login').addEventListener('click', () => {
auth0.loginWithRedirect().catch(() => {
});
});
window.addEventListener('load', () => {
auth0.handleRedirectCallback().then(redirectResult => {
auth0.getUser().then(user => {
console.log(user);
});
});
});
2 - Calling an API
<button id="call-api">Call an API</button>
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);
});
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();
});
Caching strategy
The SDK can be configured to cache ID tokens and access tokens either in memory or in local storage. The default is in memory. This setting can be controlled using the cacheStrategy
option when creating the Auth0 client.
To use the in-memory mode, no additional options need are required as this is the default setting. To configure the SDK to cache data using local storage, set cacheStrategy
as follows:
await createAuth0Client({
domain: '<AUTH0_DOMAIN>',
client_id: '<AUTH0_CLIENT_ID>',
redirect_uri: '<MY_CALLBACK_URL>',
cacheStrategy: 'localstorage'
}).then(auth0 => {
});
Important: This feature will allow the caching of data such as ID and access tokens to be stored in local storage. Exercising this option changes the security characteristics of your application and should not be used lightly. Extra care should be taken to mitigate against XSS attacks and minimize the risk of tokens being stolen from local storage.
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.