88 lines
2.3 KiB
TypeScript
88 lines
2.3 KiB
TypeScript
import { Component, OnInit, NgZone, AfterViewInit, ElementRef } from '@angular/core';
|
|
import { GoogleSignInSuccess } from 'angular-google-signin';
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
import { TokenService, TokenProvider } from '../services/login/token.service';
|
|
|
|
@Component({
|
|
selector: 'login-page',
|
|
templateUrl: './login-page.html',
|
|
providers: []
|
|
})
|
|
export class LoginComponent implements OnInit, AfterViewInit {
|
|
returnUrl: string;
|
|
token: any;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private ngZone: NgZone,
|
|
private tokenService: TokenService,
|
|
private element: ElementRef
|
|
) {
|
|
|
|
}
|
|
|
|
ngOnInit() {
|
|
// reset login status
|
|
//this.authenticationService.logout();
|
|
|
|
// get return url from route parameters or default to '/'
|
|
|
|
//this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
|
|
}
|
|
//private myClientId: string = '524432312250-vhgidft856v8qftsc81kls4c74v87d8o.apps.googleusercontent.com';
|
|
private myClientId: string = '1010962018903-glegmqudqtl1lub0150vacopbu06lgsg.apps.googleusercontent.com';
|
|
|
|
public auth2: any;
|
|
|
|
private scope = [
|
|
'profile',
|
|
'email'
|
|
].join(' ');
|
|
|
|
public googleInit() {
|
|
|
|
gapi.load('auth2', () => {
|
|
this.auth2 = gapi.auth2.init({
|
|
client_id: this.myClientId,
|
|
cookie_policy: '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());
|
|
|
|
this.ngZone.run(() => this.router.navigateByUrl('tabs'));
|
|
|
|
window.location.reload();
|
|
|
|
}, function (error) {
|
|
console.log(JSON.stringify(error, undefined, 2));
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
this.googleInit();
|
|
}
|
|
|
|
|
|
|
|
}
|