I’m new to google OAuth. I’m trying to store the credentials of a user, but i end up getting this error:
[GSI_LOGGER]: Store credential failed: {}
It seems like when the user signs in the with google button, it’s working because I am receiving a JSON Web Token, and the button displays the user’s email address and photo. But i can’t seem to store the credentials properly.
Am i storing the credentials properly? Here is my code:
function onSignIn(responsePayload) {
let cred = {id: responsePayload.sub, credential: responsePayload.credential};
// Stores the credential using Google Identity Services
google.accounts.id.storeCredential(cred);
console.log("ID: " + responsePayload.sub);
console.log('Full Name: ' + responsePayload.name);
console.log('Given Name: ' + responsePayload.given_name);
console.log('Family Name: ' + responsePayload.family_name);
console.log("Image URL: " + responsePayload.picture);
console.log("Email: " + responsePayload.email);
// Redirect to the welcome page
//window.location.href = '/welcome.html';
}
window.handleCredentialResponse = (response) => {
console.log("The response is >>>", response);
function decodeJWTResponse(){
const base64Url = response.credential.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
const responsePayload = decodeJWTResponse(response.credential);
console.log("ID: " + responsePayload.sub);
console.log('Full Name: ' + responsePayload.name);
console.log('Given Name: ' + responsePayload.given_name);
console.log('Family Name: ' + responsePayload.family_name);
console.log("Image URL: " + responsePayload.picture);
console.log("Email: " + responsePayload.email);
emailAddress = responsePayload.email;
onSignIn(responsePayload);
}
and i am rendering the google button using html:
<div id="g_id_onload"
data-client_id="{my-client-id}"
data-context="signin"
data-callback="handleCredentialResponse"
data-ux_mode="popup"
data-login_uri="{my-login-uri}"
data-auto_prompt="false">
</div>
<div class="g_id_signin"
data-type="standard"
data-shape="rectangular"
data-theme="outline"
data-text="signin_with"
data-size="large"
data-logo_alignment="left">
</div>
Any ideas why I am getting this error? Am i using google.accounts.id.storeCredential()
improperly? Any help is greatly appreciated! Thanks in advance and happy coding 🙂