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 🙏

© 2025 – Pkg Stats / Ryan Hefner

petite-auth

v1.1.0

Published

Tiny JavaScript OAuth2 client

Downloads

14

Readme

Petite · Auth

:lock: Tiny (469B) JavaScript OAuth2 client. Try it live here.

This library implements OAuth2 implicit grant flow (or client-side flow).

Installation

Install package from npm:

npm install --save petite-auth

Use it in your project:

import { authorize, parseHash } from 'petite-auth';

Usage

Login

Redirect user to your OAuth2 provider authorize endpoint:

import { authorize } from 'petite-auth';

function login() {
  authorize(process.env.AUTHORIZE_URL, {
    client_id: process.env.CLIENT_ID,
    redirect_uri: process.env.CALLBACK_URL,
    response_type: 'token id_token',
    scope: 'openid profile',
  });
}

Handle authentication

When OAuth2 provider redirected to your application callback URL, parse hash and store authentication result:

import { parseHash } from 'petite-auth';

function handleAuthentication() {
  const authResult = parseHash();
  const expiresAt = JSON.stringify((authResult.expires_in * 1000) + Date.now());
  localStorage.setItem('access_token', authResult.access_token);
  localStorage.setItem('id_token', authResult.id_token);
  localStorage.setItem('expires_at', expiresAt);
}

Check if user is authenticated

function isAuthenticated() {
  const expiresAt = JSON.parse(localStorage.getItem('expires_at'));
  return Date.now() < expiresAt;
}

Logout

function logout() {
  localStorage.removeItem('access_token');
  localStorage.removeItem('id_token');
  localStorage.removeItem('expires_at');
}

Framework integration

Preact

You can use this preact-cli template to quickly get started with petite-auth.

Integration with OAuth2 providers

Auth0

Create your auth0 tenant and client on https://auth0.com/.

Add your application redirect_uri to your client Allowed Callback URLs.

import { authorize } from 'petite-auth';

function login() {
  authorize(`https://${process.env.AUTH0_DOMAIN}/authorize`, {
    client_id: process.env.AUTH0_CLIENT_ID,
    redirect_uri: process.env.CALLBACK_URL,
    response_type: 'token id_token',
    scope: 'openid profile',
  });
}

If you specified profile scope, you can fetch user profile using the userprofile endpoint:

function getProfile() {
  return fetch(`https://${process.env.AUTH0_DOMAIN}/userinfo`, {
    headers: {
      Authorization: `Bearer ${localStorage.getItem('access_token')}`
    }
  }).then(response => response.json());
}

Google

Create your application and OAuth keys on the Google developer console.

Add your application redirect_uri to the authorized callback URIs.

import { authorize } from 'petite-auth';

function login() {
  authorize('https://accounts.google.com/o/oauth2/v2/auth', {
    client_id: process.env.GOOGLE_CLIENT_ID,
    redirect_uri: process.env.CALLBACK_URL,
    response_type: 'token id_token',
    scope: 'openid profile',
  });
}

If you specified profile scope and enabled Google+ API in the developer console, you can fetch user's Google+ profile using the access token:

function getProfile() {
  return fetch('https://www.googleapis.com/plus/v1/people/me', {
    headers: {
      Authorization: `Bearer ${localStorage.getItem('access_token')}`
    }
  }).then(response => response.json());
}

Facebook

Create your application in the Facebook developer console. Add Facebook Login to it and add your application redirect_uri to the authorized callback URIs.

import { authorize } from 'petite-auth';

function login() {
  authorize('https://www.facebook.com/v2.11/dialog/oauth', {
    client_id: process.env.FACEBOOK_CLIENT_ID,
    redirect_uri: process.env.CALLBACK_URL,
    response_type: 'token',
    scope: 'public_profile',
  });
}

You can fetch user's public profile using the access token:

function getProfile() {
  return fetch('https://graph.facebook.com/me?fields=id,name,picture', {
    headers: {
      Authorization: `Bearer ${localStorage.getItem('access_token')}`
    }
  }).then(response => response.json());
}

License

MIT