forked from github/plane
8a95a41100
Co-authored-by: Bavisetti Narayan <72156168+NarayanBavisetti@users.noreply.github.com> Co-authored-by: NarayanBavisetti <narayan3119@gmail.com> Co-authored-by: Bavisetti Narayan <narayan@Bavisettis-MacBook-Pro.local> Co-authored-by: Nikhil <118773738+pablohashescobar@users.noreply.github.com> Co-authored-by: M. Palanikannan <73993394+Palanikannan1437@users.noreply.github.com> Co-authored-by: Lakhan Baheti <94619783+1akhanBaheti@users.noreply.github.com> Co-authored-by: Dakshesh Jain <65905942+dakshesh14@users.noreply.github.com> Co-authored-by: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com>
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { FC, CSSProperties, useEffect, useRef, useCallback, useState } from "react";
|
|
|
|
import Script from "next/script";
|
|
|
|
export interface IGoogleLoginButton {
|
|
text?: string;
|
|
handleSignIn: React.Dispatch<any>;
|
|
styles?: CSSProperties;
|
|
}
|
|
|
|
export const GoogleLoginButton: FC<IGoogleLoginButton> = ({ handleSignIn }) => {
|
|
const googleSignInButton = useRef<HTMLDivElement>(null);
|
|
const [gsiScriptLoaded, setGsiScriptLoaded] = useState(false);
|
|
|
|
const loadScript = useCallback(() => {
|
|
if (!googleSignInButton.current || gsiScriptLoaded) return;
|
|
|
|
(window as any)?.google?.accounts.id.initialize({
|
|
client_id: process.env.NEXT_PUBLIC_GOOGLE_CLIENTID || "",
|
|
callback: handleSignIn,
|
|
});
|
|
|
|
try {
|
|
(window as any)?.google?.accounts.id.renderButton(
|
|
googleSignInButton.current,
|
|
{
|
|
type: "standard",
|
|
theme: "outline",
|
|
size: "large",
|
|
logo_alignment: "center",
|
|
width: 360,
|
|
text: "signin_with",
|
|
} as any // customization attributes
|
|
);
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
|
|
(window as any)?.google?.accounts.id.prompt(); // also display the One Tap dialog
|
|
|
|
setGsiScriptLoaded(true);
|
|
}, [handleSignIn, gsiScriptLoaded]);
|
|
|
|
useEffect(() => {
|
|
if ((window as any)?.google?.accounts?.id) {
|
|
loadScript();
|
|
}
|
|
return () => {
|
|
(window as any)?.google?.accounts.id.cancel();
|
|
};
|
|
}, [loadScript]);
|
|
|
|
return (
|
|
<>
|
|
<Script src="https://accounts.google.com/gsi/client" async defer onLoad={loadScript} />
|
|
<div className="overflow-hidden rounded w-full" id="googleSignInButton" ref={googleSignInButton} />
|
|
</>
|
|
);
|
|
};
|