Giter Club home page Giter Club logo

simple-redirect's Introduction

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>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.