@gaagul/react-auth-kit
Version:
A flexible authentication package for React.
3 lines (2 loc) • 1.41 kB
JavaScript
import{useState as e,useRef as o,useCallback as n,useEffect as t}from"react";const r=({useFedcm:r=!0,onSuccess:c=()=>{},onError:i=()=>{},clientId:l=""}={})=>{const[d,u]=e(!1),a=o(null),s=o(null),g=n((e=>{e&&e.credential?c&&"function"==typeof c&&c(e):i&&"function"==typeof i&&i(new Error("Invalid credential response"))}),[c,i]);t((()=>{if(!s.current){const e=document.createElement("div");e.style.display="none",document.body.appendChild(e),s.current=e}const e=document.createElement("script");return e.src="https://accounts.google.com/gsi/client",e.async=!0,e.defer=!0,document.body.appendChild(e),window.onGoogleLibraryLoad=()=>{var e;window.google?(window.google.accounts.id.initialize({client_id:l,callback:g,use_fedcm_for_prompt:r,use_fedcm_for_button:r}),window.google.accounts.id.renderButton(s.current,{type:"standard",shape:"rectangular",theme:"outline",text:"continue_with",size:"large",logo_alignment:"left"}),window.google.accounts.id.prompt(),a.current=null===(e=s.current)||void 0===e?void 0:e.querySelector("div[role='button']"),u(!0)):console.error("Google library failed to load")},()=>{document.body.removeChild(e),s.current&&document.body.removeChild(s.current)}}),[l,g,r]);return{signIn:n((()=>{a.current?a.current.click():console.error("Google Sign-In button not initialized yet")}),[]),isGoogleReady:d}};export{r as default,r as useGoogleLogin};
//# sourceMappingURL=index.esm.js.map