Everything is the same in previous answered except I added
declare var gapi: any; otherwise, you will get the error.
src/index.html
In the index.html file of your app you need to add this in the section:
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
typings/browser/ambient/gapi/
You need to add gapi & gapi.auth2 to your typings:
npm install --save @types/gapi.auth2
npm install --save @types/gapi
(see this borysn's question to understand this a little better).
src/app/+login/login.component.ts
This is the file of my component, here you need to use the ngAfterViewInit() to use the gapi an get the auth. And you can follow the implementation here developers.google...sign-in/web/build-button
As an example, this is my template:
<div id="my-signin2"></div>
and sign in function:
declare var gapi: any;
ngAfterViewInit() {
gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'light',
'onsuccess': param => this.onSignIn(param)
});
}
public onSignIn(googleUser) {
var user : User = new User();
((u, p) => {
u.id = p.getId();
u.name = p.getName();
u.email = p.getEmail();
u.imageUrl = p.getImageUrl();
u.givenName = p.getGivenName();
u.familyName = p.getFamilyName();
})(user, googleUser.getBasicProfile());
((u, r) => {
u.token = r.id_token;
})(user, googleUser.getAuthResponse());
user.save();
this.goHome();
};