Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
react-gsi
Advanced tools
Readme
React bindings for the Sign in With Google for Web API.
npm install --save react-gsi @types/gsi
To enable Sign In With Google on your website, you first need to set up your Google API client ID.
You must have a client ID to configure Sign In With Google and to verify ID tokens on your backend.
A client ID looks like the following example:
1234567890-abc123def456.apps.googleusercontent.com
import {
GsiButton,
GsiClient,
IdTokenProvider,
useIdToken,
useOneTap
} from 'react-gsi';
const idConfiguration: IdConfiguration = {
client_id: '1234567890-abc123def456.apps.googleusercontent.com',
auto_select: true // automatically sign in, see: https://developers.google.com/identity/gsi/web/guides/automatic-sign-in-sign-out
}
const buttonConfiguration: GsiButtonConfiguration = {
type: 'standard',
theme: 'outline',
size: 'large'
}
export function App() {
return (
<GsiClient fallback={<LoadingSpinner />}>
<IdTokenProvider configuration={configuration}>
<Page/>
</IdTokenProvider>
</GsiClient>
)
}
function Page() {
const token = useIdToken();
const signedOut = token === null;
useOneTap({
show: signedOut
})
if (signedOut) {
return (
<>
<h1>Logged Out</h1>
<GsiButton configuration={buttonConfiguration} />
</>
);
} else {
const { select_by, credential } = token;
return (
<>
<h1>Logged In via {select_by}</h1>
<p>{credential}</p>
</>
)
}
}
<GsiClient>
The <GsiClient>
component loads the client library.
Fallbacks can be provided whilst the library is loading or if it has failed to load.
Once loaded, the Sign In With Google JavaScript API will be
accessible via google.accounts.id
.
function LoadingFallback() {
return <span>Loading...</span>
}
function ErrorFallback() {
return <span>Error</span>
}
function App() {
return (
<GsiClient loading={LoadingFallback} error={ErrorFallback}>
Library Loaded
</GsiClient>
);
}
<IdTokenProvider>
The <IdTokenProvider>
initializes the API with the supplied
IdConfiguration
.
When the API invokes the callback
to indicate a successful
sign-in, the ID Token returned is stored and passed to the children of the
<IdTokenProvider>
via an <IdTokenContext>
.
Children may access the token in the current context by using the
useIdToken()
hook.
const idConfiguration: IdConfiguration = {
client_id: '1234567890-abc123def456.apps.googleusercontent.com'
}
function App() {
return (
<GsiClient>
<IdTokenProvider configuration={idConfiguration}>
<Page />
</IdTokenProvider>
</GsiClient>
);
}
function Page() {
const token = useIdToken();
...
}
<GsiButton>
The <GsiButton>
will render the Sign in with Google button.
const buttonConfiguration: GsiButtonConfiguration = {
type: 'standard',
theme: 'outline',
size: 'large'
}
function App() {
return (
<GsiClient>
<GsiButton configuration={buttonConfiguration} />
</GsiClient>
);
}
useGsiClient()
The useGsiClient()
hook loads the client library.
The status of the script can be accessed via the return type.
Once loaded, the Sign In With Google JavaScript API will be
accessible via google.accounts.id
.
function App() {
const { status } = useGsiClient();
switch (status.type) {
case 'idle':
return <span>Idle...</span>;
case 'loading':
return <span>Loading...</span>;
case 'loaded':
return <Page />;
case 'error':
return <span>Error</span>;
}
}
useOneTap()
The useOneTap()
hook controls the One Tap flow.
The flow can begin by calling prompt
, and can be stopped by calling cancel.
By default, the prompt will show automatically on mount. This can be changed by
setting the show
flag to false
.
function App() {
const { prompt, cancel } = useOneTap({
show: false // don't show on mount
});
return (
<>
<button type="button" onClick={prompt}>Prompt</button>
<button type="button" onClick={cancel}>Cancel</button>
</>
)
}
Bug reports and pull requests are welcome on GitHub.
This project is available under the terms of the ISC license. See the
LICENSE
file for the copyright information and licensing terms.
FAQs
React bindings for the 'Sign in With Google for Web' API
The npm package react-gsi receives a total of 12 weekly downloads. As such, react-gsi popularity was classified as not popular.
We found that react-gsi demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.