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

@macabrrra/easy-auth-js

v1.0.0

Published

OAuth authorization code with PKCE for SPAs

Downloads

7

Readme

EasyAuth - OAuth Simplified for JavaScript SPAs

EasyAuth is a lightweight, easy-to-use JavaScript library designed to simplify the implementation of OAuth authentication in Single Page Applications (SPAs). It abstracts the complexities of OAuth flows, particularly for handling the Authorization Code flow with PKCE, making it easier for developers to securely implement user authentication.

Features

  • Easy integration with OAuth 2.0 authorization servers.
  • Supports Authorization Code flow with PKCE.
  • Handles token storage and retrieval.
  • Simplifies token refresh and expiration checks.

Installation

To install EasyAuth, use npm:

npm install easy-auth-js

Usage

Here's a basic guide on how to use EasyAuth in your application:

Initialize EasyAuth

First, import and initialize the EasyAuth class with your configuration.

import { EasyAuth } from 'easy-auth-js';

const easyAuth = new EasyAuth({
  authorizeEndpoint: 'https://example.com/oauth2/authorize',
  tokenEndpoint: 'https://example.com/oauth2/token',
  clientID: 'your-client-id',
  redirectURI: 'https://yourapp.com/callback',
  scopes: ['openid', 'profile', 'email']
});

Authorize

Invoke the authorize method to redirect the user to the OAuth provider's authorization page.

Typically, you would add a sign-in button that, when selected, redirects the user to the configured OAuth redirect URL. Under the hood, the authorization server's API is called.

easyAuth.authorize();

Handle Authentication Response

After the user is redirected back to your application, use getAuth() to handle the authentication response.

Example:

easyAuth.getAuth().then(tokenData => {
  // Handle successful authentication
  console.log(tokenData);
}).catch(error => {
  // Handle errors
  console.error(error);
});

Logout

To log out the user and clear tokens, use the logout method.

easyAuth.logout();

Handling Token Expiration

EasyAuth provides a callback mechanism that can be used to define custom behavior when the token becomes invalid or expires. This is particularly useful to manage user experience when a session ends or to initiate a token refresh process.

Setting Up the Token Expiration Callback

Use the onTokenInvalid method to set a callback that will be triggered when the token is detected as invalid or expired.

easyAuth.onTokenInvalid(() => {
  // Custom logic when the token is invalid or expired
  easyAuth.authorize();
});

Contributing

Contributions to EasyAuth are welcome! Please refer to the CONTRIBUTING.md file for guidelines.

License

EasyAuth is MIT licensed. See the LICENSE file for details.

Disclaimer

This library is provided "as is", without warranty of any kind. Use at your own risk.