
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
gapi-oauth-react-hooks
Advanced tools
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.
The package exposes its own declaration files; you won't need to install any @types/* if you use typescript.
To install, use either yarn or npm:
yarn add gapi-oauth-react-hooks
npm i gapi-oauth-react-hooks
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 } from "gapi-oauth-react-hooks";
export const Login = () => {
const {
state,
signedUser,
authResponse,
onSignIn,
onSignOut
} = useGoogleAuth();
const display = {
Loading: <>Well, gapi is being loaded...</>,
SignedIn: <SignedIn user={signedUser} authResponse={authResponse} onSignOut={onSignOut} />,
NotSignedIn: <SimpleButton onClick={onSignIn} text="Login" />,
Errored: <>Oh no!</>,
};
return <>{display[state]}</>;
};
interface SignedInProps {
onSignOut: () => Promise<void>;
user?: gapi.auth2.BasicProfile;
authResponse?: gapi.auth2.AuthResponse;
}
const SignedIn: React.FC<SignedInProps> = ({ onSignOut, user, authResponse }) => (
<>
<div>user {JSON.stringify(user)}</div>
<div>auth response {JSON.stringify(authResponse)}</div>
<SimpleButton onClick={onSignOut} text="Logout" />
</>
);
This package exposes three entities:
A static class to hold gapi configuration
This class contains a config function that takes three parameters:
An interface defining the states Gapi can be at
This interface has the following members:
A react hook to handle signin and signout using gapi auth2
This hook returns an object containing:
FAQs
Some hooks for SSO using Google sign in
The npm package gapi-oauth-react-hooks receives a total of 1 weekly downloads. As such, gapi-oauth-react-hooks popularity was classified as not popular.
We found that gapi-oauth-react-hooks demonstrated a not healthy version release cadence and project activity because the last version was released 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.