I have been trying to implement Google's identity services API in my React app, but couldn't get it to work.
I'm trying to implement the following really simple JS code: https://developers.google.com/identity/gsi/web/guides/display-button#javascript
What I came up with is the following:
useEffect(() => {
//I'm adding the <script>
const elemScript = document.createElement('script');
elemScript.src = "https://accounts.google.com/gsi/client";
elemScript.async = true;
elemScript.defer = true;
document.body.append(elemScript);
//adding the code from the documentation
window.onload = function () {
/*global google*/
console.log(google)
google.accounts.id.initialize({
client_id: "<don't worry, I put the ID here>",
callback: handleCredentialResponse
});
google.accounts.id.renderButton(
googleButton.current, //this is a ref hook to the div in the official example
{ theme: "outline", size: "large" } // customization attributes
);
}
return () => {
//I get ride of the <scripts> when the elements is unmounted
document.body.removeChild(elemScript);
}
}, [])
//This is my main and only objective, to get the token...
function handleCredentialResponse(response) {
console.log("Encoded JWT ID token: " + response.credential);
}
return (
<div ref={googleButton}></div>
)
}
export default GoogleAuth
When I call this component in my main App it sometimes renders the button, sometimes it doesn't (and the cause of this seems to be react-router-dom as it won't load if I'm moving from another page in my domain to where the button is). And even when I get the render I get errors and I can't login.
Thanks to Bogdanof's answer solved the problem of the button only appearing sometimes through the use of Promises.
Now my only problem is the following:
[GSI_LOGGER]: The given origin is not allowed for the given client ID.
I have made and ID OAuth2.0 on Google only for this App. I enabled http://localhost:3000 as it's JS origin (and the same for the URIs), but I'm still getting that answer. I tried changing browsers and even deleting my cache and cookies as I read that it may help, but nothing worked.
Someone has any ideas about how to solve this?