Skip to content

Instantly share code, notes, and snippets.

@trashvin
Last active June 2, 2017 12:51
Show Gist options
  • Save trashvin/7fb152e13a452047391d0ede612598af to your computer and use it in GitHub Desktop.
Save trashvin/7fb152e13a452047391d0ede612598af to your computer and use it in GitHub Desktop.
Logging in Using Google Account and Getting User Data (option 1 Angular 2)
<!--Add this on the head tag -->
<script src="https://apis.google.com/js/platform.js" async defer></script>
import { Component } from '@angular/core';
declare const gapi: any;
@Component({
moduleId: module.id,
selector: 'login',
templateUrl: 'login.component.html'
})
export class LoginComponent {
public auth2:any
ngAfterViewInit() {
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.init({
client_id: '386098344927-ab4200uubl901f6vfage52sjtrj3l977.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
scope: 'profile email'
});
this.attachSignin(document.getElementById('glogin'));
});
}
public attachSignin(element : any) {
this.auth2.attachClickHandler(element, {},
(loggedInUser : any) => {
console.log("Test" + JSON.stringify (loggedInUser));
//instead of console.log, yougo ahead and save it to localStorage
}, function (error : any) {
// alert(JSON.stringify(error, undefined, 2));
});
}
}
<!--Add this on the login page -->
<button id="glogin" class="btn btn-primary">Sign In Using Google</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment