Passage.js

Use our JavaScript library directly to build custom authentication flows.

Client SDK

Passage.js is our JavaScript library that is a building block for all Passage authentication and user management. This library contains primitives for adding user registration and login functionality to your website.

All Passage Elements use Passage.js and are the recommended way to add authentication to your website, but you can also build directly with Passage.js if you have custom requirements or use-cases that don't fit within an element. The library references are available here:

Setup

Via NPM

Install the @passageidentity/passage-js package via NPM.

npm install @passageidentity/passage-js

Then you can import Passage.js into your code as follows:

import { Passage } from '@passageidentity/passage-js';

const passage = new Passage('YOUR APP ID');
const userInfo = await passage.identifierExists('[email protected]')
if (userInfo.user === null) {
  //register
} else {
  //login
}

Via CDN

Include the PassageJS library on your website as follows:

<script type="module">
    import {Passage} from "https://cdn.passage.id/passage-js.js"

    const psg = new Passage('YOUR_APP_ID');
</script>

With Token Store

The base Passage constructor takes an optional config parameter that allows you to define your own token store. If you would like to define custom methods for where to get, set, or clear auth related tokens you should use this pattern. See the Token Store Reference for more details.

tokenStore.js

import { TokenStore } from '@passageidentity/passage-js';

export class MyTokenStore extends TokenStore {
    getAuthToken() {
        const jwt = localStorage.getItem('passage_jwt')
        return Promise.resolve(JSON.parse(jwt));
    }
    setTokens(authResult){
        const passageToken = authResult.auth_token;
        localStorage.setItem('passage_jwt', passageToken);
        return Promise.resolve();
    }
};
import { Passage } from '@passageidentity/passage-js';
import { MyTokenStore } from 'tokenStore.js';
const tokenStore = new MyTokenStore();

const passage = new Passage('YOUR APP ID', {tokenStore});

References

pageAuthentication ReferencepageUser ReferencepageSession ReferencepagePassageError Class

Last updated