Reputation: 1204
Below code works fine but when opened from an incognito windows console only shows
GoogleYolo loaded.
No credentials
<html>
<head><title>My Yolo Login example</title>
<script src="/common.js"></script>
<script defer src="https://smartlock.google.com/client"></script>
<script>
const PUBLIC_CLIENT_ID =
'*******-**********.apps.googleusercontent.com';
const CONFIG = {
supportedAuthMethods: [
'googleyolo://id-and-password',
'https://accounts.google.com'
],
supportedIdTokenProviders: [{
uri: 'https://accounts.google.com',
clientId: PUBLIC_CLIENT_ID
}],
context:"signUp"
};
window.onGoogleYoloLoad = (googleyolo) => {
console.log("GoogleYolo loaded.");
var retrievePromise = googleyolo.retrieve(CONFIG);
retrievePromise.then((credential) => {
console.log(credential);
},
(error)=>{
if(error.type == "noCredentialsAvailable"){
console.log("No credential");
googleyolo.hint(CONFIG).then((credentials)=>{console.log(credentials);});}
});
};
</script>
</head>
</html>
How can I know that user doesn't have a google account and redirect/take other action for him to login to his google account.
Upvotes: 4
Views: 2915
Reputation: 11
window.onGoogleLibraryLoad = () => {
// initialize one-tap here
google.accounts.id.initialize({
client_id: "YOUR_ID.apps.googleusercontent.com",
callback: handleCredentialResponse,
itp_support: true,
auto_selec: true,
prompt_parent_id: 'g_id_onload',
cancel_on_tap_outside: false,
ux_mode: 'popup',
});
google.accounts.id.prompt((notification) => {
// if one-tap is not displayed show the google sign-in button (in private mode for example)
if (notification.isNotDisplayed()) {
console.log('not displayed');
google.accounts.id.renderButton(document.getElementById('g_id_onload'), {
type: 'standard',
shape: 'rectangular',
theme: 'outline',
text: 'continue_with',
size: 'large',
logo_alignment: 'center',
width: 300
})
}
});
};
function handleCredentialResponse(googleResponse) {
// do something after callback from Google
}
<script defer async src='https://accounts.google.com/gsi/client'></script>
<div id='g_id_onload'></div>
Upvotes: 1
Reputation: 3880
I'm the product manager at Google for this library. We are working on some better support for users who are not signed in, but in the interim, when no Google Accounts are active and no credentials are available, you can simply show the traditional Google Sign-In button and have the user click to proceed through the flow to activate their Google Account in the browser.
https://developers.google.com/identity/sign-in/web/
Upvotes: 4