I'm trying to add a login/logout to/from google by their guide: https://developers.google.com/identity/sign-in/web/sign-in
But I'm facing some problems.
index.html:
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id" content="**my-google-api-key**.apps.googleusercontent.com">
<script>
gapi.load('auth2',function () {
gapi.auth2.init();
});
</script>
app.component.html:
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<a href="#" onclick="signOut();">Sign out</a>
app.component.ts:
public onSignIn(googleUser):void {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
}
public signOut():void {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
Problems:
- After logging in succussfully, onSignIn function does not get called so nothing is printed but the signIn is working.
- In the signOut function I have error: "Cannot find name 'gapi'." but the signout is working.
Question:
Google tells us not to use the googleUser.getBasicProfile().getId() as the user ID but use the ID Token intead: googleUser.getAuthResponse().id_token.sub. Why?