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

@open-pioneer/authentication

v2.3.0

Published

Authentication API for Open Pioneer Trails applications.

Downloads

100

Readme

@open-pioneer/authentication

This package provides a central service to handle the current user's session. It cannot be used on its own, but requires an authentication plugin, that implements the actual authentication flow.

Usage

Retrieving the current authentication state

To inspect the current authentication state, inject a reference to the AuthService by referencing "authentication.AuthService".

The methods getAuthState() and getSessionInfo() return information about the current state:

const authService = ...; // injected

// Returns SessionInfo if the user is currently logged in, or undefined otherwise.
const sessionInfo = await authService.getSessionInfo();

// Like the above, but synchronous and includes intermediate states like "pending".
const state = authService.getAuthState();

// Use `on("changed", ...)` to be notified about changes.
const handle = authService.on("changed", () => {
    const newState = authService.getAuthState();
});

// Don't forget to clean up event handles in the future
handle.destroy();

Enforcing authentication

To make sure that only users, that are logged in, can use an application, enforce the authentication flow by wrapping the application with the <ForceAuth /> component:

// AppUI.jsx
import { ForceAuth } from "@open-pioneer/authentication";

export function AppUI() {
    return (
        <ForceAuth>
            <TheRestOfYourApplication />
        </ForceAuth>
    );
}

ForceAuth renders its children (your application) if the user is authenticated. Otherwise, it renders the authentication plugin's fallback component (see below). It is updated if the authentication state changes.

Fallback

If the user is not logged in, a fallback is shown to the user. The fallback must be implemented in the authentication plugin. Depending on the implementation of the authentication plugin, a fallback can be a login prompt, or a simple message. Some plugins do not provide a visual fallback but an "effect" instead: an action to perform, such as a redirect to the authentication provider.

Rendering of the login fallback can be customized by passing custom properties (fallbackProps) or by supplying a custom render function (renderFallback), see the API documentation.

Triggering logout

To explicitly end the current session, call the AuthService's logout() method:

const authService = ...; // injected
authService.logout();

Implementing an authentication plugin

An authentication plugin (providing authentication.AuthPlugin) must be present in the application to support authentication. The plugin implements the actual authentication flow.

The plugin must implement the AuthPlugin TypeScript interface exported by this package:

  • Provide the current authentication state by implementing getAuthState(). When authenticated, a user's authentication state contains session information, such as the user's id, an optional display name and arbitrary additional attributes that can be defined by the plugin.

    If the state changes internally (for example successful login, explicit logout, logout due to timeout, etc.), the changed event must be emitted to notify the AuthService.

  • Return the login behavior value (a React component or a function to call) by implementing getLoginBehavior(). This could be a login dialog, a "forbidden" message ("fallback") or a function implementing a redirect ("effect").

  • Implement the logout() method: this method is called when the user attempts to end their session.

An example is available in this project's auth-sample.

License

Apache-2.0 (see LICENSE file)