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

gapi-oauth-react-hooks

v3.4.10

Published

Some hooks for SSO using Google sign in

Downloads

63

Readme

gapi-oauth-react-hooks

Open in Visual Studio Code npm bundle size Github workflow Quality Gate Status Maintainability Rating Security Rating Reliability Rating Coverage Coverage Lines of Code Technical Debt Code Smells Bugs Vulnerabilities Duplicated Lines (%) Last commit

Google SSO hooks for react applications.

⚡ Purpose

I needed SSO for google users and wasn't quite satisfied with what I found in the react eco system. Perhaps this will be useful for someone else, so here we go :rocket:

The package exposes its own declaration files; you won't need to install any @types/* if you use typescript.

⚡ Installation

To install, use either yarn or npm:

yarn add gapi-oauth-react-hooks
npm i gapi-oauth-react-hooks

⚡ Typical use

It's best to setup the config early, perhaps in the index or app file:

import { GapiConfig } from "gapi-oauth-react-hooks";

// pulling from .env here
GapiConfig.setup(
  process.env.GOOGLE_AUTH_CLIENTID,
  process.env.GOOGLE_AUTH_SCOPE,
  process.env.GOOGLE_AUTH_REDIRECTURI
);

ReactDOM.render(<Login />, document.getElementById("root"));

Now, let's use the main hook in our Login component:

import { useGoogleAuth, UserProfile } from "gapi-oauth-react-hooks";

export const Login = () => {
  const auth = useGoogleAuth();

  const display = {
    Errored: <>Oh no!</>,
    Loading: <>Loading ...</>,
    NotSignedIn: <button onClick={auth.onSignIn} >Login</button>,
    SignedIn: <SignedIn {...auth} />
  };

  return <>{display[auth.state]}</>;
};

interface SignedInProps {
  onSignOut: () => Promise<void>;
  signedUser?: UserProfile;
  authResponse?: gapi.auth2.AuthResponse;
}

const SignedIn: React.FC<SignedInProps> = ({ onSignOut, signedUser, authResponse }) => (
  <>
    <div>user {JSON.stringify(signedUser)}</div>
    <div>auth response {JSON.stringify(authResponse)}</div>
    <button onClick={onSignOut} >Logout</button>
  </>
);

⚡ Api

This package exposes two functions as well as two types:

🔶 Types

🌀 GapiState

This type defines gapi state.

| Value | Description | | ----------- | -------------------------------------- | | Loading | gapi is not ready yet | | Errored | an error occured while loading gapi | | SignedIn | gapi is ready and a user is signed in | | NotSignedIn | gapi is ready and no user is signed in |

🌀 UserProfile

This type defines user data properties.

| Property | Description | | ---------- | -------------------- | | id | the id of the user | | email | the user email | | familyName | the user family name | | givenName | the user given name | | name | the user name | | imageUrl | the user avatar |

🌀 GoogleAuthHookProps

This type defines what returns the useGoogleAuth hook.

| Property | Description | | ------------ | ---------------------------- | | state | The gapi state | | signedUser | The signer user (duh) | | authResponse | The auth response | | onSignIn | A function initiating login | | onSignOut | A function initiating logout |

🔶 Functions

🌀 GapiConfig.setup

This static class contains a config function that takes three parameters. Once called, useGoogleAuth can be used.

import { GapiConfig } from 'gapi-oauth-react-hooks';

GapiConfig.setup(clientId, scope, redirectUri);

| Parameter | Description | | ----------- | ------------------- | | clientId | The gapi client id | | scope | The requested scope | | redirectUri | The redirect Uri |

🌀 useGoogleAuth

This react hook handles signin and signout using gapi auth2.

import { useGoogleAuth, GoogleAuthHookProps } from 'gapi-oauth-react-hooks';

const {
  state,
  signedUser,
  authResponse,
  onSignIn,
  onSignOut,
}: GoogleAuthHookProps = useGoogleAuth();