react-new-google-login
v0.1.5
Published
React New Google Login component
Downloads
4
Readme
New Google Login for React
The Google Sign-In JavaScript Platform Library for web has been deprecated and will not work for existing uses after Mar 31, 2023 nor for newly created Google Cloud projects.
This React component creates a Google Login button using the new Google Identity Service for Web introduced in early 2022 and on successful login generates a JWT that can be used to authenticate other Google or custom services.
Installation
npm install --save react-new-google-login
Usage
import {useState} from 'react';
import GoogleLogin from 'react-new-google-login';
const App = () => {
const [token, setToken] = useState('');
const signin = (token) => setToken(token);
const error = (err) => console.log(err);
const CLIENT_ID = 'google-client-id-abc123'
return (
<div className="login-button">
<GoogleLogin
clientId={CLIENT_ID}
signinCallback={signin}
errorCallback={error}
options={options}
className={className}
/>
<div>{token}</div>
</div>
)
}
Properties
clientId (string)
The Client ID obtained from Google Cloud Console.
- Select the project
- Select APIs & Services
- Select Credentials
- Create an OAuth2 Client
- Copy the Client ID string
- Be sure to add all URL origins that will be using the Google Signin Button. Include
http://localhost:PORT
ANDhttp://localhost
className
Class to be added to the Google button outside wrapper. To center the login button in a use the following CSS in the enclosing :
.login-button {
height: 45px;
width: fit-content;
margin: 0 auto;
}
signinCallback
Signature: signinCallback(token: string) => void
Callback function fired with a successful login. The function receives one string parameter representing the JWT returned from the Google Authentication.
errorCallback
Signature: (err: any) => void
Error function is fired on a login error. The error object is passed as the only function parameter.
options
See full reference for GsiButtonConfiguration
type
Button configuration options (see full documentation)
type
:standard
for full-size button oricon
for a Google icon only (required)theme
:outline
|filled_blue
|filled_black
size
:large
|medium
|small
text
:signin_with
|signup_with
|continue_with
|signup_with
shape
:rectangular
|pill
|circle
|square
logo_alignment
:left
|center
width
: maximum width of the buttonlocale
: for button localization