import { HostConfiguration } from '../../app.constants'; import { LoginProviderConfiguration } from './LoginProviderConfiguration'; import { AuthService } from '../../services/auth/auth.service'; import { LoginOptions } from './LoginOptions'; import { LoginServiceConfiguration } from './LoginServiceConfiguration'; import { LoginProviders } from '../../models/login/LoginInfo'; import { Optional, NgZone, Injectable } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { MatSnackBar } from '@angular/material'; import { SnackBarNotificationComponent } from '../../shared/components/notificaiton/snack-bar-notification.component'; import { Router, ActivatedRoute, Params } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; declare const gapi: any; declare const FB: any; declare const IN: any; @Injectable() export class LoginService { private providers: LoginOptions[] private auth2: any; constructor( private router: Router, public authService: AuthService, public route: ActivatedRoute, public snackBar: MatSnackBar, public language: TranslateService, private zone: NgZone, private httpClient: HttpClient @Optional() private config: LoginServiceConfiguration, ) { if (config) { this.providers = config.loginProviders; } else this.providers = [LoginOptions.nativeLogin]; } public initProviders() { if (this.hasProvider(LoginOptions.googleOauth)) this.initialiseGoogleOauth(); if (this.hasProvider(LoginOptions.facebookOauth)) this.initialiseFacebookOauth(); } public hasProvider(provider: LoginOptions) { for (let i = 0; i < this.providers.length; i++) { if (provider === this.providers[i]) return this.isProviderProperlyConfigured(provider) } return false; } private isProviderProperlyConfigured(provider: LoginOptions) { switch (provider) { case LoginOptions.facebookOauth: return this.hasAllRequiredFieldsConfigured(this.config.facebookConfiguration); case LoginOptions.googleOauth: return this.hasAllRequiredFieldsConfigured(this.config.googleConfiguration) case LoginOptions.linkedInOauth: return this.hasAllRequiredFieldsConfigured(this.config.linkedInConfiguration); case LoginOptions.twitterOauth: return this.hasAllRequiredFieldsConfigured(this.config.twitterConfiguration); case LoginOptions.nativeLogin: return true; default: throw new Error("Unsupported Provider Type") } } private hasAllRequiredFieldsConfigured(configuration: LoginProviderConfiguration) { if (configuration != null && configuration.clientId != null) return true return false; } /* * GOOGLE SIGN IN */ private initialiseGoogleOauth(): void { gapi.load('auth2', () => { this.auth2 = gapi.auth2.init({ client_id: this.config.googleConfiguration.clientId, cookiepolicy: 'single_host_origin', scope: 'profile email' }); this.attachGoogleSignin(document.getElementById('googleSignInButton')); }); } public attachGoogleSignin(element) { if (!element) return this.auth2.attachClickHandler(element, {}, (googleUser) => { var id_token = googleUser.getAuthResponse().id_token; if (id_token) { this.authService.login({ ticket: id_token, provider: LoginProviders.Google }).subscribe( res => this.onLogInSuccess(res), error => this.onLogInError(error) ) } }, (error) => { alert(JSON.stringify(error, undefined, 2)); }); } /* * FACEBOOK SIGN IN */ private initialiseFacebookOauth(): void { FB.init({ appId: this.config.facebookConfiguration.clientId, cookie: false, xfbml: true, version: 'v2.8' }); } public facebookLogin() { FB.login((response: any) => { if (response.status === 'connected' || 'not_authorized') { this.authService.login({ ticket: response.authResponse.accessToken, provider: LoginProviders.Facebook }).subscribe( res => this.onLogInSuccess(res), error => this.onLogInError(error) ) } }, { scope: 'user_friends,email' }); } /* * LINKEDIN SIGN IN */ public linkedinAuthorize() { window.location.href = this.config.linkedInConfiguration.oauthUrl + "?response_type=code&client_id=" + this.config.linkedInConfiguration.clientId + "&redirect_uri=" + this.config.linkedInConfiguration.redirectUri + "&state=987654321" } public linkedInInitialiseLogin() { this.router.navigate(["/login/linkedin"]) } public linkedInloginUser(code: string) { this.authService.login({ ticket: code, provider: LoginProviders.LinkedIn }).subscribe( res => this.onLogInSuccess(res), error => this.onLogInError(error) ) } /* * LOGIN HANDLERS */ 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'] }) } }