Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@auth0/auth0-spa-js
Advanced tools
Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE
@auth0/auth0-spa-js is a JavaScript library designed to handle authentication in single-page applications (SPAs) using Auth0. It provides a simple and secure way to integrate Auth0's authentication and authorization services into your SPA.
Login
This feature allows users to log in to your application using Auth0. The code sample demonstrates how to create an Auth0 client and initiate a login redirect.
const auth0 = await createAuth0Client({
domain: 'YOUR_DOMAIN',
client_id: 'YOUR_CLIENT_ID'
});
await auth0.loginWithRedirect({
redirect_uri: window.location.origin
});
Logout
This feature allows users to log out of your application. The code sample shows how to log out and redirect the user to the home page.
await auth0.logout({
returnTo: window.location.origin
});
Get User Information
This feature retrieves the authenticated user's information. The code sample demonstrates how to get the user profile after authentication.
const user = await auth0.getUser();
console.log(user);
Token Handling
This feature handles token retrieval for authenticated requests. The code sample shows how to get an access token silently without redirecting the user.
const token = await auth0.getTokenSilently();
console.log(token);
oidc-client is a JavaScript library for OpenID Connect (OIDC) and OAuth2. It provides similar functionalities for handling authentication and authorization in SPAs. Compared to @auth0/auth0-spa-js, oidc-client is more generic and can be used with any OIDC-compliant identity provider.
Firebase Authentication provides backend services to help authenticate users in your application. It supports various authentication methods, including email/password, phone, and social providers. Unlike @auth0/auth0-spa-js, Firebase offers a broader range of services beyond authentication, such as database and hosting.
Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE.
From the CDN:
<script src="https://cdn.auth0.com/js/auth0-spa-js/1.7.0-beta.4/auth0-spa-js.production.js"></script>
Using npm:
npm install @auth0/auth0-spa-js
Using yarn:
yarn add @auth0/auth0-spa-js
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 => {
//...
});
<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);
});
});
});
<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);
});
});
<button id="logout">Logout</button>
import createAuth0Client from '@auth0/auth0-spa-js';
document.getElementById('logout').addEventListener('click', () => {
auth0.logout();
});
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' // valid values are: 'memory' or '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.
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
This SDK is in Early Access with selected stakeholders.
We process feedback and provide support via private channels.
For a rundown of common issues you might encounter when using the SDK, please check out the FAQ.
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Auth0 helps you to easily:
This project is licensed under the MIT license. See the LICENSE file for more info.
v1.7.0-beta.4 (2020-03-03)
Changed
Fixed
FAQs
Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE
The npm package @auth0/auth0-spa-js receives a total of 0 weekly downloads. As such, @auth0/auth0-spa-js popularity was classified as not popular.
We found that @auth0/auth0-spa-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 45 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.