2017-10-16 17:05:25 +02:00
|
|
|
import { Component, OnInit, ElementRef, AfterViewInit, VERSION } from '@angular/core';
|
|
|
|
import { TokenService, TokenProvider } from '../../services/login/token.service';
|
|
|
|
import {Router} from '@angular/router';
|
|
|
|
|
|
|
|
declare const gapi: any;
|
|
|
|
|
2017-10-27 09:48:33 +02:00
|
|
|
import '../../../assets/custom.js';
|
|
|
|
declare function simple_notifier(type: string, title: string, message:string): any;
|
2017-10-16 17:05:25 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'googgle-sign-in',
|
|
|
|
templateUrl: './googgle-sign-in.component.html',
|
|
|
|
styleUrls: ['./googgle-sign-in.component.css']
|
|
|
|
})
|
|
|
|
export class GooggleSignInComponent implements OnInit, AfterViewInit {
|
|
|
|
|
2017-10-27 09:48:33 +02:00
|
|
|
//token:any;
|
2017-10-16 17:05:25 +02:00
|
|
|
|
|
|
|
constructor(private element: ElementRef, private tokenService : TokenService, private router : Router) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
}
|
|
|
|
|
2018-01-18 11:27:30 +01:00
|
|
|
private clientId:string = '524432312250-sc9qsmtmbvlv05r44onl6l93ia3k9deo.apps.googleusercontent.com';
|
2017-10-16 17:05:25 +02:00
|
|
|
|
|
|
|
private scope = [
|
|
|
|
'profile',
|
|
|
|
'email'
|
|
|
|
//,
|
|
|
|
//'https://www.googleapis.com/auth/plus.me',
|
|
|
|
//'https://www.googleapis.com/auth/contacts.readonly',
|
|
|
|
//'https://www.googleapis.com/auth/admin.directory.user.readonly'
|
|
|
|
].join(' ');
|
|
|
|
|
|
|
|
public auth2: any;
|
|
|
|
|
|
|
|
public googleInit() {
|
|
|
|
|
|
|
|
gapi.load('auth2', () => {
|
|
|
|
this.auth2 = gapi.auth2.init({
|
|
|
|
client_id: this.clientId,
|
|
|
|
cookiepolicy: 'single_host_origin',
|
|
|
|
scope: this.scope
|
|
|
|
});
|
|
|
|
var buttonElement = this.element.nativeElement.querySelector('#googleBtn');
|
|
|
|
this.attachSignin(buttonElement);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public attachSignin(element) {
|
|
|
|
this.auth2.attachClickHandler(element, {},
|
|
|
|
(googleUser) => {
|
2017-10-27 09:48:33 +02:00
|
|
|
simple_notifier("success",null,"Successful login");
|
2017-10-16 17:05:25 +02:00
|
|
|
let profile = googleUser.getBasicProfile();
|
2017-10-27 09:48:33 +02:00
|
|
|
this.tokenService.login(googleUser.getAuthResponse().id_token, TokenProvider.google, profile.getName(), profile.getEmail());
|
2017-10-16 17:05:25 +02:00
|
|
|
}, function (error) {
|
2017-10-27 09:48:33 +02:00
|
|
|
simple_notifier("danger",null,"Failed to login");
|
2017-10-16 17:05:25 +02:00
|
|
|
console.log(JSON.stringify(error, undefined, 2));
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
this.googleInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
signOut() {
|
|
|
|
var auth2 = gapi.auth2.getAuthInstance();
|
|
|
|
auth2.signOut().then(function () {
|
|
|
|
console.log('User signed out.');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|