npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jpz95/auth0-spa-js

v1.3.1

Published

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

Downloads

4

Readme

@jpz95/auth0-spa-js

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

License

Table of Contents

API Docs

Installation

Using npm:

npm install @jpz95/auth0-spa-js

Using yarn:

yarn add @jpz95/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 '@jpz95/auth0-spa-js';

//with async/await
const auth0 = await createAuth0Client({
  domain: 'example.somesite.com', // we will prepend 'https://'
  authorizeEndpoint: '/path/to/auth', // forms https://example.somesite.com/path/to/auth
  tokenEndpoint: '/path/to/token', // forms https://example.somesite.com/path/to/token
  client_id: '<API_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>'
});

//or, you can just instantiate the client on it's own
import { Auth0Client } from '@jpz95/auth0-spa-js';

const auth0 = new Auth0Client({
  domain: 'example.somesite.com', // we will prepend 'https://'
  authorizeEndpoint: '/path/to/auth', // forms https://example.somesite.com/path/to/auth
  tokenEndpoint: '/path/to/token', // forms https://example.somesite.com/path/to/token
  client_id: '<API_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>'
});

//if you do this, you'll need to check the session yourself
try {
  await getTokenSilently();
} catch (error) {
  if (error.error !== 'login_required') {
    throw error;
  }
}

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);
});

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);
});


### 3 - Logout

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

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

Data caching options

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 cacheLocation 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 cacheLocation as follows:

await createAuth0Client({
  domain: 'example.somesite.com', // we will prepend 'https://'
  authorizeEndpoint: '/path/to/auth', // forms https://example.somesite.com/path/to/auth
  tokenEndpoint: '/path/to/token', // forms https://example.somesite.com/path/to/token
  client_id: '<API_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>',
  cacheLocation: 'localstorage' // valid values are: 'memory' or 'localstorage'
});

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.

Refresh Tokens

Refresh tokens can be used to request new access tokens. Read more about how our refresh tokens work for browser-based applications to help you decide whether or not you need to use them.

To enable the use of refresh tokens, set the useRefreshTokens option to true:

await createAuth0Client({
  domain: 'example.somesite.com', // we will prepend 'https://'
  authorizeEndpoint: '/path/to/auth', // forms https://example.somesite.com/path/to/auth
  tokenEndpoint: '/path/to/token', // forms https://example.somesite.com/path/to/token
  client_id: '<API_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>',
  useRefreshTokens: true
});

Using this setting will cause the SDK to automatically request a new refresh token from the authorization server. Refresh tokens will then be used to exchange for new access tokens instead of using a hidden iframe, and calls the token endpoint directly. This means that in most cases the SDK does not rely on third-party cookies when using refresh tokens.

Refresh Token fallback

In all cases where a refresh token is not available, the SDK falls back to the legacy technique of using a hidden iframe with prompt=none to try and get a new access token and refresh token. This scenario would occur for example if you are using the in-memory cache and you have refreshed the page. In this case, any refresh token that was stored previously would be lost.

If the fallback mechanism fails, a login_required error will be thrown and could be handled in order to put the user back through the authentication process.

Note: This fallback mechanism does still require access to the Auth0 session cookie, so if third-party cookies are being blocked then this fallback will not work and the user must re-authenticate in order to get a new refresh token.

Support + Feedback

For support or to provide feedback, please raise an issue on our issue tracker.

Frequently Asked Questions

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

License

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