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

@forward-software/react-auth

v1.1.0

Published

Simplify your Auth flow when working with React apps

Downloads

7

Readme

React Auth

Simplify your Auth flow when working with React apps

license CI Language grade: JavaScript Coverage Status Github Issues

npm NPM downloads

This React package allows you to streamline the integration of user authentication flows in any React app by providing a single unified interface


Install

yarn add @forward-software/react-auth

Setup

Define an AuthClient class

Create a new AuthClient class which extends the BaseAuthClient provided by this library and implements the 4 required methods:

  • onInit, called when the AuthClient gets initialized
  • onLogin, invoked when the login method of the AuthClient gets called
  • onRefresh, invoked when the refresh method of the AuthClient gets called
  • onLogout, invoked when the logout method of the AuthClient gets called
import { BaseAuthClient } from '@forward-software/react-auth';

// The type for your credentials
type AuthCredentials = {
  username: string;

  password: string;
};

// The type for your tokens
type AuthTokens = {
  authToken: string;

  refreshToken: string;
};

class AuthClient extends BaseAuthClient<AuthTokens, AuthCredentials> {
  protected onInit(): Promise<void> {
    // Implement the initialization logic for your client
  }

  protected onLogin(credentials?: AuthCredentials): Promise<AuthTokens> {
    // Implement the logic required to exchange the provided credentials for user tokens
  }

  protected onRefresh(minValidity?: number): Promise<AuthTokens> {
    // Implement the logic required to refresh the current user tokens
  }

  protected onLogout(): Promise<void> {
    // Implement the logic required to invalidate the current user tokens
  }
}

Instantiate an AuthClient

Create an instance of the AuthClient class defined

const authClient = new AuthClient();

AuthClient Props

  • isInitialized, a boolean indicating if the AuthClient has been initialized
  • isAuthenticated, a boolean indicating if the login process has been successfull and the user is authenticated
  • tokens, the current tokens returned by the login or the refresh process

AuthClient Methods

Core
  • init(), initialize the AuthClient (N.B. this shouldn't be called if using AuthProvider - see below)
  • login(credentials), start the login process
  • refresh(), refresh the current tokens
  • logout(), logout and invalidate the current tokens
EventEmitter
  • on(eventName, listenerFn), subscribe to eventName events emitted by the AuthClient
  • off(eventName, listenerFn), unsubscribe from eventName events emitted by the AuthClient
Observable
  • subscribe(() => { }), subscribe to AuthClient state changes
  • getSnapshot(), returns the current state of the AuthClient

React components

Setup React components to interact with the AuthClient using the createAuth function exported by this library

import { createAuth } from '@forward-software/react-auth';

export const { AuthProvider, useAuthClient } = createAuth(authClient);

the createAuth function returns:

  • AuthProvider, the context Provider component that should wrap your app and provide access to your AuthClient
  • useAuthClient, the hook to retrieve and interact with your AuthClient

AuthProvider

The context Provider component that should wrap your app and provide access to your AuthClient, this component also accepts 2 additional props

  • ErrorComponent, displayed when the AuthClient initialization fails
  • LoadingComponent, displayed while the AuthClient is being initialized

Examples

The examples folder contains some examples of how you can integrate this library in your React app.

Credits

This library has been inspired by react-keycloak and similar libraries.

License

MIT


Made with ✨ & ❤️ by ForWarD Software and contributors

If you found this project to be helpful, please consider contacting us to develop your React and React Native projects.