argos/dmp-frontend/src/app/login/login.component.ts

76 lines
2.5 KiB
TypeScript

import { HttpClient } from '@angular/common/http';
import { Component, OnInit, ElementRef, AfterViewInit, VERSION, Injectable, NgZone } from '@angular/core';
import { Router, ActivatedRoute, Params } from "@angular/router";
import { MatPaginator, MatSort, MatSnackBar } from "@angular/material";
import { TranslateService } from "@ngx-translate/core";
import { AuthService } from '../services/auth/auth.service';
import { SnackBarNotificationComponent } from '../shared/components/notificaiton/snack-bar-notification.component';
declare const gapi: any;
@Component({
selector: 'login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
public auth2: any;
constructor(private router: Router,
public authService: AuthService,
public route: ActivatedRoute,
public snackBar: MatSnackBar,
public language: TranslateService,
private zone: NgZone
) { }
ngOnInit() {
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.init({
client_id: '524432312250-sc9qsmtmbvlv05r44onl6l93ia3k9deo.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
scope: 'profile email'
});
this.attachGoogleSignin(document.getElementById('googleSignInButton'));
});
}
public attachGoogleSignin(element) {
this.auth2.attachClickHandler(element, {},
(googleUser) => {
var id_token = googleUser.getAuthResponse().id_token;
if (id_token) {
this.authService.login({ ticket: id_token, service: "google" }).subscribe(
res => this.onLogInSuccess(res),
error => this.onLogInError(error)
)
}
}, (error) => {
alert(JSON.stringify(error, undefined, 2));
});
}
public onLogInSuccess(logoutMessage: any) {
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-LOGIN', language: this.language },
duration: 3000,
extraClasses: ['snackbar-success']
});
this.route.queryParams.subscribe((params: Params) => {
let redirectUrl = params['returnUrl'] ? params['returnUrl'] : '/';
this.zone.run(() => this.router.navigate([redirectUrl]));
})
}
public onLogInError(errorMessage: string) {
console.log(errorMessage);
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
data: { message: 'GENERAL.SNACK-BAR.UNSUCCESSFUL-LOGIN', language: this.language },
duration: 3000,
extraClasses: ['snackbar-warning']
})
}
}