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

@kalicloud/oauthify

v0.3.0

Published

A package for handling Oauth2.0 flows in React, with fully custom oauth flow handlers

Downloads

16

Readme

Oauthify

A package for handling Oauth2.0 flows in React, with fully custom oauth flow handlers

Installation

Install the package using npm:

   npm install @kalicloud/oauthify

or using yarn:

yarn add @kalicloud/oauthify

Build

Build the package using the following command:

npm run build

Usage

import React, { useState } from'react';
import { Oauth2 } from '@kalicloud/oauthify';

function App(): React.FC {
   const [ error, setError ] = React.useState<string | any>();

   const signIn = (oauth_data: any) => {
       /**
        * The oauth login was successful
        */
   }

   const Error = (error: string | any) {
       setError(error)
   }

   return (
       <div className="Login-page">
           <div className="login-error">
               <p>{error}</p>
           </div>
           <Oauth2
               id='<ID>'
               className='<BUTTON_CLASSNAME>'
               provider="<PROVIDER>"
               apiKey="<API_KEY" 
               clientId="<CLIENT_ID>" 
               clientSecret="<CLIENT_SECRET>"
               syncAuthOnServer={true} // This will sync the authentication status with the server (Optional)
               syncOptions={{server_start_url: "http://localhost:3000/social/start", server_end_url: "http://localhost:3000/social/end"}} // This will tell Oauthify where to start and end the authentication process
               state="<STATE>"
               redirectUri="<REDIRECT_URI>"
               mode="<MODE>"
               responseType="<RESPONSE_TYPE>"
               scope="<SCOPES>"
               onSuccess={signIn} 
               onError={Error}
           />
       </div>                 
   )
}

Authentication Modes

There are two types of authentication modes, Redirect and Popup.

Popup

Popup is an mode of authentication, where the user is redirected to a popup window, instead of the default browser window.

The Oauth2 component will handle the authentication process, and will return the user's access_token and refresh_token.

Redirect

Redirect is an authentication mode where the user is redirected to the redirectUri, which is set in the Oauth2 component. This mode is the most secure and most flexible, but it is also the most streamlined way to use Oauth2. As it does not require additional client side handling.

Example of an handler (Redirection handler)

import React, { useState, useEffect } from'react';
import { useParams } from 'react-router-dom';
import { Oauth2, redirect_mode_hook, user_hook } from '@kalicloud/oauthify';

function App(): React.FC {
    const [ error, setError ] = React.useState<string | any>();
    const [ access_token, setAccessToken ] = React.useState<string | any>();
    const [ refresh_token, setRefreshToken ] = React.useState<string | any>(); // If the application supports refresh tokens, most do
    const [ user, setUser ] = React.useState<string | any>();

    const params = useParams();

    const onSuccess = (oauth_data: any) => {
    /**
     * The oauth login was successful
     * 
    */

        const user = new user_hook()

        // Get the oauth Provider
        const provider = user.checkProvider()

        // Get the provider's name
        const name = provider.id

        alert(`You have now logged in with: ${name} and your email is: ${oauth_data.email}`)
        setUser(oauth_data.email)
}

const Error = (error: string | any) => {
setError(error)
}

    useEffect(() => {    
        redirect_mode_hook({
            onSuccess,
            Error
            });
    }, [])

    return (
        <div className="Login-page">
            <h1>Validating the oauth was successful</h1>
            {error && <p>{error}</p>}
            {user && <p>You are now authticated as: {user}</p>}
        </div>
        )
    }

The redirect_mode_hook function will perform all the necessary steps for the authentication process under the hood. And sends back the User Information to the onSuccess function if successful.

You don't have to write your own authentication handler, you just handle the response from the redirect_mode_hook component in your app.

Don't forget to pass the onSuccess and onError props to the redirect_mode_hook component. This is required, as the redirect_mode_hook component will handle the authentication process. additionally Make sure that the code above is wrapped in the redirect url, so /:client/redirect (The client must be the first param in the url!) will be the redirect url. Required

Example of an handler (Popup handler)

import React, { useEffect } from'react';
import { user_hook, isPopup, handle_popup_exit } from '@kalicloud/oauthify';

function App(): React.FC {
    useEffect(() => {
        // Popup mode
        if (isPopup()) {
            handle_popup_exit()
        } 
    }, [])

    return (
        <h1>This window will close now...</h1>
    )
}

export default App;

The handle_popup_exit function will perform all the necessary steps for the authentication process under the hood. And sends back the User Information to the onSuccess function from the caller window if successful. The current window will close after the authentication process is finished.

Authentication Synchronization

You may want to use Oauthify to authenticate your users on your own server backend, once Oauthify has successfully authenticated the user via the Oauth2 provider.

This is now possible within the brand new v1.3.2 version of Oauthify.

You can now authenticate the user on your own server backend so the user can access protected routes on the server.

What's new

  • v1.3.2 Server Synchronization support
  • Provider Kalicloud is now updated and supported with the brand new authentication endpoints.
  • updated the README.md file with more detailed instructions.
  • Popup mode is supported, with custom hooks for maximum flexibility and customization.
  • Mutiple auth methods are supported,
  • Added Redirection hook, which will perform all the necessary steps for the authentication process under the hood. And sends back the user_information to the onSuccess function if successful.
  • Added support for client_secret
  • Added support for unique state to be validated to prevent CSRF attacks.
  • Added support for response_type which is used to determine the type of response that the application will receive.
  • Added support for scope which is used to determine what data the application will access.

Features

  • Fully customizable
  • Fully typed
  • Fully documented
  • Fully tested
  • Easy API authentication under the hood
  • Easy to use
  • No manual interaction required/ handling

Coming soon

- Add support for more providers
- Add support for Logout functionality
- Add support for Auth Status Check    
- Add support for Refreshing Tokens 
- Add support for Custom Oauth2.0 Implementations

Builtin oauth providers

We have included a select few of built in providers, which you can use by passing the provider name as a string.

here is a dedication list of the providers we have included:

  • Google
  • Microsoft
  • Facebook
  • Kalicloud (Default)

Each provider will require some sort of Client ID and Client Secret to be used, some providers also require the use of an API Key

By default it will use the Kalicloud provider, but you can change this by passing the provider name as a string, to the provider prop.

Redirect URL will also need to be provided, this is the URL that the user will be redirected to after completing the oauth flow.

List of Providers that I won't support in the future, unless they update their APIs and Authentication Standards.

  • Github
  • Twitch

Roadmap

  • Add more hooks to handle the authentication process and user experience
  • Custom Components For each provider

Note

This package is still in development, and is not yet ready for production use, be aware that some api usage might not be stable or not yet implemented.