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

89 lines
2.1 KiB
TypeScript

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;
@Component({
selector: 'googgle-sign-in',
templateUrl: './googgle-sign-in.component.html',
styleUrls: ['./googgle-sign-in.component.css']
})
export class GooggleSignInComponent implements OnInit, AfterViewInit {
token:any;
constructor(private element: ElementRef, private tokenService : TokenService, private router : Router) {
}
ngOnInit() {
}
private clientId:string = '1010962018903-glegmqudqtl1lub0150vacopbu06lgsg.apps.googleusercontent.com';
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) => {
this.token = googleUser.getAuthResponse().id_token;
let profile = googleUser.getBasicProfile();
this.tokenService.setLoggedIn(true);
this.tokenService.setToken(this.token);
this.tokenService.setProvider(TokenProvider.google);
this.tokenService.setUsername(profile.getName());
this.tokenService.setEmail(profile.getEmail());
window.location.reload();
}, function (error) {
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.');
});
}
*/
}