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

oauth-spa-js

v0.8.2

Published

Single-Page Application Authorization Client for the OAuth 2.0 PKCE flow

Downloads

9

Readme

OAuth 2.0 | Single-Page Applications | Authorization Client

A JS-SDK for gaining access to applications and API's implementing OAuth 2.0 within Single-Page Applications (inspired by Auth0). The client obtains access tokens by using the PKCE (Proof Key for Code Exchange) authorization flow for public clients.

Getting Started

Installation

Using npm in your project directory run the following command:

npm install oauth-spa-js

Integrate authentication into your application

Create the client and have it globally accessible in your application (verify authorization- and token endpoint urls with your authentication provider):

const resources: OAuthResource[] = [
    {
        is_user_information_resource: true,
        identifier: RESOURCE_IDENTIFIER,
        scopes: ["User.Read"]
    },
]

const client: OAuthClient = createOAuthClient({
    client_id: PUBLIC_APP_ID,
    resources,
    authorization_endpoint: `${SERVER_URL}/authorize`,
    token_endpoint: `${SERVER_URL}/token`,
    logout_endpoint: `${SERVER_URL}/logout`,
    revoke_endpoint: `${SERVER_URL}/revoke`,
    introspect_endpoint: `${SERVER_URL}/introspect`,
    user_info_endpoint: USER_INFO_ENDPOINT,
})

Redirect the user to the authorization provider page:

const redirect_uri = `${window.location.origin}/oauth/callback`
const state = window.location.href
await client.loginWithRedirect({ redirect_uri, state })

Create a callback directory which will handle the callback from the authorization page:

// https://example.com/oauth/callback
const { origin, pathname } = window.location
const redirect_uri = `${origin}${pathname}`

const searchParams = new URLSearchParams(window.location.search);
const state = searchParams.get("state")

try {
    await client.handleRedirectCallback({ redirect_uri })
} catch(e) {
    // Handle error
}

window.location.href = state ?? "/"

And get the access token from the client (the token is automatically refreshed by the method if expired):

try {
    const access_token = await client.getAccessToken(RESOURCE_IDENTIFIER)
} catch(e) {
    // Handle refresh access token error
}

Subscribe to the authorization state of the client:

client.subscribe(RESOURCE_IDENTIFIER, async token => {
    is_authorized = client.isAuthorized(RESOURCE_IDENTIFIER)
    access_token = token
    user_info = await client.getUserInfo()
})

Check out the oauth-spa-js Github repository for a simple example developed in SvelteKit.

References