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

87 lines
2.3 KiB
TypeScript

import { AuthService } from '../../services/auth/auth.service';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit, ElementRef, AfterViewInit, VERSION, Injectable } from '@angular/core';
import { Router, ActivatedRoute, Params } from "@angular/router";
import { SnackBarNotificationComponent } from "../../shared/components/notificaiton/snack-bar-notification.component";
import { MatPaginator, MatSort, MatSnackBar } from "@angular/material";
import { TranslateService } from "@ngx-translate/core";
declare const gapi: any;
declare var auth2 :any;
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']
})
export class GooggleSignInComponent implements OnInit, Injectable {
constructor(private element: ElementRef, private router : Router,private authService:AuthService,private route:ActivatedRoute,
public snackBar: MatSnackBar,public language: TranslateService
) { }
ngOnInit() {
this.initiateExternalProviders();
}
initiateExternalProviders(){
// if(gapi.auth2 == undefined){
// gapi.load('auth2', () => {
// this.auth2 = gapi.auth2.getAuthInstance({
// client_id: clientId,
// })
// });
// }else{
// this.auth2=gapi.auth2.getAuthInstance({
// client_id: clientId,
// })}
}
public signIn() {
auth2.grantOfflineAccess().then((authResult)=>this.signInCallback(authResult))
}
signInCallback(authResult){
if (authResult['code']) {
this.authService.login({ticket:authResult['code'],service:"google"}).subscribe(
res => this.onLogInSuccess(res),
error => this.onLogInError(error)
)
}
}
public onLogInSuccess(logoutMessage: any) {
this.route.queryParams.subscribe((params: Params) => {
let redirectUrl = params['returnUrl'] ? params['returnUrl'] : '/';
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']
})
}
}