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

simple-oauth-redirect

v1.1.0

Published

Provides simple functions for initiating and handling local OAuth redirects for both Node and Browser

Downloads

94

Readme

Simple OAuth Redirect

A simple package for navigating to an OAuth endpoint, receiving the callback, and returning the callback URL all in one async function.

Supports both Node and browsers, both ESM and CommonJS, and TypeScript.

Version

npm i simple-oauth-redirect

How to use in Node

Import the Node implementation with simple-oauth-redirect/node. The function will create a simple http server that binds to the callback URL for the duration of the promise.

const SimpleOAuth = require('simple-oauth-redirect/node');
// import { redirect } from "simple-oauth-redirect/node";

const callbackUrl = 'http://localhost:8000/callback';
const targetUrl = `http://example.com/oauth?redirect_uri=${encodeURIComponent(callbackUrl)}`;

SimpleOAuth.redirect(targetUrl, callbackUrl).then((url) => {
    console.log('OAuth callback url with codes:', url);
});

How to use in browser

Import the browser implementation with simple-oauth-redirect/web. The browser implementation also uses the Broadcast Channel API.

The callback page must call checkForCallback() for the redirect promise to return.

Example Svelte Implementation

index.svelte

<script lang="ts">
  import { redirect } from 'simple-oauth-redirect/web';

  let responseUrl: string;

  async function authButton() {
    const callbackUrl = `${window.location.origin}/callback`;
    const targetUrl = `http://example.com/oauth?redirect_uri=${encodeURIComponent(callbackUrl)}`;
    const res = await redirect(targetUrl, callbackUrl);
    responseUrl = res.href;
  }
</script>

{#if !responseUrl}
  <button on:click={() => authButton()}>Authorize</button>
{:else}
  <h1>Received URL:</h1>
  <p>{responseUrl}</p>
{/if}

callback.svelte

<script>
  import { onMount } from 'svelte';
  import { checkForCallback } from 'simple-oauth-redirect/web';

  onMount(() => {
    checkForCallback();
  });
</script>

Example Vanilla HTML Implementation

index.html

<html lang="en">
    <script type="module">
        import { redirect } from 'https://cdn.skypack.dev/simple-oauth-redirect/web';
        document.getElementById('auth-button').addEventListener('click', () => {
            const callbackUrl = `${window.location.origin}/callback.html`;
            const targetUrl = `http://example.com/oauth?redirect_uri=${encodeURIComponent(callbackUrl)}`;

            redirect(targetUrl, callbackUrl).then((url) => {
                alert('OAuth callback url with codes: ' + url);
                console.log('OAuth callback url with codes:', url);
            });
        });
    </script>
    <body>
        <button id="auth-button">Authorize</button>
    </body>
</html>

callback.html

<html lang="en">
    <script type="module">
        import { checkForCallback } from 'https://cdn.skypack.dev/simple-oauth-redirect/web';
        checkForCallback();
    </script>
</html>