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

larapassport-spa-js

v1.0.2

Published

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

Downloads

9

Readme

larapassport-spa-js

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

This package has been inspired by @auth0/auth0-spa-js and laravel-passport-spa-js.

Installation

Using npm:

npm install larapassport-spa-js

Getting Started

Laravel Passport Configuration

You need a working authentication server which is up and running (e.g.: http://localhost:8000).

Follow the official documention's steps to create a client.

Make sure that you have a public Passport client with the correct callback URL. The callback URL should reflect the origins that your SPA application is running on. The URL may also include a path, depending on where you're handling the callback (e.g.: http://localhost:3000/callback).

Also, make sure the CORS are set properly.

Take note of the Client ID value. You'll need this values in the next step.

Creating the client

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

import createAuthClient from 'larapassport-spa-js';

//with async/await
const authClient = await createAuthClient({
  domain: '<LARAVEL_PASSPORT_DOMAIN>',
  client_id: '<LARAVEL_PASSPORT_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>'
});

//with promises
createAuthClient({
  domain: '<LARAVEL_PASSPORT_DOMAIN>',
  client_id: '<LARAVEL_PASSPORT_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>'
}).then(authClient => {
  //...
});

Using createAuthClient does a couple of things automatically:

  • It creates an instance of AuthClient.
  • It calls getTokenSilently to refresh the user session.
  • It suppresses all errors from getTokenSilently, except login_required.
//or, you can just instantiate the client on it's own
import { AuthClient } from 'larapassport-spa-js';

const authClient = new AuthClient({
  domain: '<LARAVEL_PASSPORT_DOMAIN>',
  client_id: '<LARAVEL_PASSPORT_CLIENT_ID>',
  redirect_uri: '<MY_CALLBACK_URL>'
});

You can also create the client directly using the AuthClient constructor. This can be useful if:

  • You wish to bypass the call to getTokenSilently on initialization.
  • You wish to do custom error handling.
  • You wish to initialize the SDK in a synchronous way

1 - Login

Make sure that your authentication server should have a login and logout web page. For that, you can use its Breeze package.

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

//redirect to the Login page
document.getElementById('login').addEventListener('click', async () => {
  await authClient.loginWithRedirect({
    redirect_uri: '<MY_CALLBACK_URL>'
    appState: '<a relative SPA path where you want to return to after login>'
  });
});

The appState is an optional parameter.

When the browser is redirected from Authentication server back to your SPA, handleRedirectCallback must be called in order to complete the login flow.

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

//with promises

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

//in your callback route (<MY_CALLBACK_URL>)
window.addEventListener('load', () => {
  authClient.handleRedirectCallback().then(redirectResult => {
    //logged in. you can get the user profile like this:
    authClient.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 tokenResult = await authClient.getTokenSilently();
  const result = await fetch('https://your.api.authentication.server.example.com', {
    method: 'GET',
    headers: {
      Accept: 'application/json',
      Authorization: `Bearer ${tokenResult.access_token}`
    }
  });
  const data = await result.json();
  console.log(data);
});

//with promises
document.getElementById('call-api').addEventListener('click', () => {
  authClient
    .getTokenSilently()
    .then(tokenResult =>
      fetch('https://your.api.authentication.server.example.com', {
        method: 'GET',
        headers: {
          Accept: 'application/json',
          Authorization: `Bearer ${tokenResult.access_token}`
        }
      })
    )
    .then(result => result.json())
    .then(data => {
      console.log(data);
    });
});

3 - Logout

Make sure that you have a logout route in your authentication server which accepts GET method calls.

<button id="logout">Logout</button>
import createAuthClient from 'larapassport-spa-js';

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

You can redirect users back to your app after logging out. This logic must be implemented in your authentication server's logout method that handles a returnTo parameter.

authClient.logout({
  returnTo: 'https://your.spa.url.example.com/'
});

You can revoke all access tokens and refresh tokens which belong to the Client ID. For that, you need to pass an allDevices paramater:

authClient.logout({
  allDevices: true
});

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 AuthClient.

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 createAuthClient({
  domain: '<LARAVEL_PASSPORT_DOMAIN>',
  client_id: '<LARAVEL_PASSPORT_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 access tokens and refresh 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.

Advanced options

Advanced options can be set by specifying the advancedOptions property when configuring AuthClient.

createAuthClient({
  domain: '<LARAVEL_PASSPORT_DOMAIN>',
  client_id: '<LARAVEL_PASSPORT_CLIENT_ID>',
  advancedOptions: {
    defaultScope: 'foo' // change the scopes that are applied to every authz request.
  }
});

Documentation

Client options

When you create the AuthClient instance, you can pass several options. Some are required, others are optional and come with default value if not specified.

const authClientOptions = {
  /**
   * REQUIRED
   * Your Laravel Passport authentication domain url such as `'your.api.authentication.server.com'`.
   */
  domain: string;

  /**
   * REQUIRED
   * The Client ID from the Laravel Passport server.
   */
  client_id: string;

  /**
   * REQUIRED
   * The default URL where Laravel Passport will redirect your browser to with the
   * authentication result.
   */
  redirect_uri: string;

  /**
   * The default scope to be used on authentication requests.
   * Defaults to '' (empty string).
   */
  scope: string;

  /**
   * Number of days until the cookie laravelPassport.is.authenticated will expire.
   * Defaults to 1.
   */
  sessionCheckExpiryDays: number;

  /**
   * If true, the SDK will use a cookie when storing information about the auth
   * transaction while the user is going through the authentication flow on the
   * authorization server. The default is `false`, in which case the SDK will use
   * session storage.
   * Defaults to `false`.
   */
  useCookiesForTransactions: boolean;
};

const authClient = await createAuthClient(authClientOptions);

Available methods

Get Access Token with no interaction

$('#getToken').click(async () => {
  const token = await authClient.getTokenSilently();
});

Get user

$('#getUser').click(async () => {
  const user = await authClient.getUser();
});

Support and Feedback

For support or to provide feedback, please raise an issue on the GitHub page.

License

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