2022-12-19 14:43:43 +00:00
|
|
|
import { FC, CSSProperties, useEffect, useRef, useCallback, useState } from "react";
|
2022-11-24 14:53:50 +00:00
|
|
|
// next
|
2022-11-19 14:21:26 +00:00
|
|
|
import Script from "next/script";
|
|
|
|
|
|
|
|
export interface IGoogleLoginButton {
|
|
|
|
text?: string;
|
2023-01-26 18:12:20 +00:00
|
|
|
handleSignIn: React.Dispatch<any>;
|
2022-11-19 14:21:26 +00:00
|
|
|
styles?: CSSProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const GoogleLoginButton: FC<IGoogleLoginButton> = (props) => {
|
2023-01-26 18:12:20 +00:00
|
|
|
const { handleSignIn } = props;
|
|
|
|
|
2022-12-16 14:56:25 +00:00
|
|
|
const googleSignInButton = useRef<HTMLDivElement>(null);
|
2022-12-19 14:43:43 +00:00
|
|
|
const [gsiScriptLoaded, setGsiScriptLoaded] = useState(false);
|
2022-12-16 14:56:25 +00:00
|
|
|
|
|
|
|
const loadScript = useCallback(() => {
|
2022-12-19 14:43:43 +00:00
|
|
|
if (!googleSignInButton.current || gsiScriptLoaded) return;
|
2022-12-16 14:56:25 +00:00
|
|
|
window?.google?.accounts.id.initialize({
|
|
|
|
client_id: process.env.NEXT_PUBLIC_GOOGLE_CLIENTID || "",
|
2023-01-26 18:12:20 +00:00
|
|
|
callback: handleSignIn,
|
2022-12-16 14:56:25 +00:00
|
|
|
});
|
|
|
|
window?.google?.accounts.id.renderButton(
|
|
|
|
googleSignInButton.current,
|
|
|
|
{
|
|
|
|
type: "standard",
|
|
|
|
theme: "outline",
|
|
|
|
size: "large",
|
|
|
|
logo_alignment: "center",
|
2023-03-21 07:16:12 +00:00
|
|
|
width: "410",
|
2022-12-16 14:56:25 +00:00
|
|
|
text: "continue_with",
|
|
|
|
} as GsiButtonConfiguration // customization attributes
|
|
|
|
);
|
|
|
|
window?.google?.accounts.id.prompt(); // also display the One Tap dialog
|
2022-12-19 14:43:43 +00:00
|
|
|
setGsiScriptLoaded(true);
|
2023-01-26 18:12:20 +00:00
|
|
|
}, [handleSignIn, gsiScriptLoaded]);
|
2022-12-16 14:56:25 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (window?.google?.accounts?.id) {
|
|
|
|
loadScript();
|
|
|
|
}
|
2022-12-19 14:43:43 +00:00
|
|
|
return () => {
|
|
|
|
window?.google?.accounts.id.cancel();
|
|
|
|
};
|
2022-12-16 14:56:25 +00:00
|
|
|
}, [loadScript]);
|
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-12-16 14:56:25 +00:00
|
|
|
<Script src="https://accounts.google.com/gsi/client" async defer onLoad={loadScript} />
|
2023-06-13 09:07:25 +00:00
|
|
|
<div
|
|
|
|
className="overflow-hidden rounded w-full flex justify-center items-center"
|
|
|
|
id="googleSignInButton"
|
|
|
|
ref={googleSignInButton}
|
|
|
|
/>
|
2022-11-19 14:21:26 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|