argos/dmp-frontend/src/app/login/googgle-sign-in/googgle-sign-in.component.ts

95 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-11-07 16:06:17 +01:00
import { Component, OnInit, ElementRef, AfterViewInit, VERSION, Injectable } from '@angular/core';
2017-11-01 18:18:27 +01:00
import { TokenService, TokenProvider } from '../../services/login/token.service';
import {Router} from '@angular/router';
declare const gapi: any;
2017-11-07 15:03:51 +01:00
declare var $ :any;
2017-11-01 18:18:27 +01:00
import '../../../assets/custom.js';
declare function simple_notifier(type: string, title: string, message:string): any;
@Component({
selector: 'googgle-sign-in',
templateUrl: './googgle-sign-in.component.html',
styleUrls: ['./googgle-sign-in.component.css']
})
2017-12-07 14:36:12 +01:00
export class GooggleSignInComponent implements OnInit, Injectable {
2017-11-01 18:18:27 +01:00
2017-11-07 16:06:17 +01:00
2017-11-01 18:18:27 +01:00
constructor(private element: ElementRef, private tokenService : TokenService, private router : Router) {
}
2017-12-07 14:36:12 +01:00
2017-11-01 18:18:27 +01:00
ngOnInit() {
2017-12-07 14:36:12 +01:00
this.initiateExternalProviders();
}
initiateExternalProviders(){
var clientId = '1010962018903-glegmqudqtl1lub0150vacopbu06lgsg.apps.googleusercontent.com';
var scope = [
'profile',
'email'
].join(' ');
if(gapi.auth2 == undefined){
gapi.load('auth2', () => {
gapi.auth2.init({
client_id: clientId,
cookiepolicy: 'single_host_origin',
scope: scope
}).then(()=>this.renderButton());
});
}else{
gapi.auth2.init({
client_id: clientId,
cookiepolicy: 'single_host_origin',
scope: scope
}).then(()=>this.renderButton());
}
2017-11-23 14:57:47 +01:00
2017-11-07 16:06:17 +01:00
}
2017-12-07 14:36:12 +01:00
renderButton() {
gapi.signin2.render('googleBtn');
var buttonElement = this.element.nativeElement.querySelector('#googleBtn');
2017-12-07 14:36:12 +01:00
if(buttonElement)this.attachSignin(buttonElement);
2017-11-07 15:03:51 +01:00
2017-11-01 18:18:27 +01:00
}
2017-11-07 15:03:51 +01:00
2017-11-01 18:18:27 +01:00
public attachSignin(element) {
gapi.auth2.getAuthInstance().attachClickHandler(element, {},
2017-11-01 18:18:27 +01:00
(googleUser) => {
//simple_notifier("success",null,"Successful login");
let profile = googleUser.getBasicProfile();
this.tokenService.login(googleUser.getAuthResponse().id_token, TokenProvider.google, profile.getName(), profile.getEmail());
},
function (error) {
2017-11-02 15:19:12 +01:00
//simple_notifier("danger",null,"Failed to login");
2017-11-01 18:18:27 +01:00
console.log(JSON.stringify(error, undefined, 2));
}
);
2017-11-01 18:18:27 +01:00
}
2017-11-07 15:03:51 +01:00
2017-11-01 18:18:27 +01:00
}