Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

gapi-cjs

sidmohanty11334MIT1.0.3

A gapi-script alternative which uses commonjs module

google, api, gapi, gapi-script, google-auth2, gapi-auth, google-sign-in, gapi-cjs

readme

gapi-cjs

A gapi-script alternative which uses commonjs module. Has some changes in the gapiScript so that it eliminates errors like:

  • this is set to undefined
  • jest error - import can't be used outside module
  • using of eval directly

Usage

npm i gapi-cjs

For gapi instance,

import { gapi } from 'gapi-cjs'

You can create your own useGoogleLogin hook,

import { gapi } from 'gapi-cjs';
import { useState, useEffect } from 'react';

export const useGoogleLogin = () => {
  const [user, setUser] = useState(null);

  useEffect(() => {
    gapi.load('client:auth2', () => {
      gapi.client
        .init({
          clientId:
            '<client-id>',
          scope: 'openid',
        })
        .then(() => {
          const auth = gapi.auth2.getAuthInstance();
          setUser(auth.currentUser.get().getBasicProfile());
        });
    });
  }, []);

  const signIn = async () => {
    const auth = await gapi.auth2.getAuthInstance();
    await auth.signIn();
    const { access_token, id_token } = await auth.currentUser
      .get()
      .getAuthResponse();
    return { access_token, id_token };
  };

  const signOut = async () => {
    const auth = await gapi.auth2.getAuthInstance();
    await auth.signOut();
  };

  return { user, signIn, signOut };
};