3.4.10 • Published 3 months ago

gapi-oauth-react-hooks v3.4.10

Weekly downloads
3
License
MIT
Repository
github
Last release
3 months ago

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.

ValueDescription
Loadinggapi is not ready yet
Erroredan error occured while loading gapi
SignedIngapi is ready and a user is signed in
NotSignedIngapi is ready and no user is signed in

🌀 UserProfile

This type defines user data properties.

PropertyDescription
idthe id of the user
emailthe user email
familyNamethe user family name
givenNamethe user given name
namethe user name
imageUrlthe user avatar

🌀 GoogleAuthHookProps

This type defines what returns the useGoogleAuth hook.

PropertyDescription
stateThe gapi state
signedUserThe signer user (duh)
authResponseThe auth response
onSignInA function initiating login
onSignOutA 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);
ParameterDescription
clientIdThe gapi client id
scopeThe requested scope
redirectUriThe 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();
3.4.8

3 months ago

3.4.9

3 months ago

3.4.10

3 months ago

3.4.7

1 year ago

3.4.6

1 year ago

3.4.5

1 year ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.2

2 years ago

3.4.1

3 years ago

3.4.0

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.0.0

3 years ago

2.1.0

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.5

3 years ago

2.0.6

3 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago