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

react-gsi

v1.0.2

Published

React bindings for the 'Sign in With Google for Web' API

Downloads

63

Readme

react-gsi

React bindings for the Sign in With Google for Web API.

Demo

Installation

npm install --save react-gsi @types/gsi

Usage

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>
            </>
        )
    }
}

Components

<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>
    );
}

A button that says 'Sign in with Google' with no personalized information.


Hooks

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>
        </>
    )
}

Account Chooser page


Contributing

Bug reports and pull requests are welcome on GitHub.

License

This project is available under the terms of the ISC license. See the LICENSE file for the copyright information and licensing terms.