2018-11-27 15:13:56 +01:00
|
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
|
|
import { Injectable, NgZone, Optional } from '@angular/core';
|
|
|
|
import { MatSnackBar } from '@angular/material';
|
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { environment } from '../../../environments/environment';
|
2018-01-31 17:05:49 +01:00
|
|
|
import { Credential } from '../../models/login/Credential';
|
2018-01-10 17:05:23 +01:00
|
|
|
import { LoginProviders } from '../../models/login/LoginInfo';
|
2018-11-27 15:13:56 +01:00
|
|
|
import { AuthService } from '../../services/auth/auth.service';
|
2018-01-10 17:05:23 +01:00
|
|
|
import { SnackBarNotificationComponent } from '../../shared/components/notificaiton/snack-bar-notification.component';
|
2018-08-30 13:09:36 +02:00
|
|
|
import { CultureService } from '../../utilities/culture/culture-service';
|
2018-11-27 15:13:56 +01:00
|
|
|
import { LoginOptions } from './LoginOptions';
|
|
|
|
import { LoginProviderConfiguration } from './LoginProviderConfiguration';
|
|
|
|
import { LoginServiceConfiguration } from './LoginServiceConfiguration';
|
2018-01-10 17:05:23 +01:00
|
|
|
|
|
|
|
declare const gapi: any;
|
|
|
|
declare const FB: any;
|
|
|
|
declare const IN: any;
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class LoginService {
|
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
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,
|
|
|
|
private cultureService: CultureService,
|
|
|
|
@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.b2Access: return this.hasAllRequiredFieldsConfigured(this.config.b2accessConfiguration);
|
|
|
|
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) => {
|
|
|
|
const 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) => {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TWITTER SIGN IN
|
|
|
|
*/
|
|
|
|
public twitterInitialiseLogin() {
|
|
|
|
this.router.navigate(['/login/twitter']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public twitterAuthorize() {
|
|
|
|
let headers = new HttpHeaders();
|
|
|
|
headers = headers.set('Content-Type', 'application/json');
|
|
|
|
headers = headers.set('Accept', 'application/json');
|
2018-11-27 15:13:56 +01:00
|
|
|
this.httpClient.get(environment.Server + 'auth/twitterRequestToken', { headers: headers }).subscribe((data: any) => {
|
2018-10-05 17:00:54 +02:00
|
|
|
window.location.href = this.config.twitterConfiguration.oauthUrl + '?oauth_token=' + data.payload.value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public twitterLogin(token: string, verifier: string) {
|
|
|
|
this.authService.login({ ticket: token, provider: LoginProviders.Twitter, data: verifier }).subscribe(
|
|
|
|
res => this.onLogInSuccess(res),
|
|
|
|
error => this.onLogInError(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* B2ACCESS LOG IN
|
2018-07-11 15:47:36 +02:00
|
|
|
*/
|
2018-01-10 17:05:23 +01:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
public b2AccessInitialiseLogin() {
|
|
|
|
this.router.navigate(['/api/oauth/authorized/b2access']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public b2AccessGetAuthCode() {
|
|
|
|
window.location.href = this.config.b2accessConfiguration.oauthUrl + '?response_type=code&client_id=' + this.config.b2accessConfiguration.clientId + '&redirect_uri=' + this.config.b2accessConfiguration.redirectUri + '&state=987654321&scope=USER_PROFILE';
|
|
|
|
}
|
|
|
|
|
|
|
|
public b2AccessLogin(code: String) {
|
|
|
|
let headers = new HttpHeaders();
|
|
|
|
headers = headers.set('Content-Type', 'application/json');
|
|
|
|
headers = headers.set('Accept', 'application/json');
|
2018-11-27 15:13:56 +01:00
|
|
|
this.httpClient.post(environment.Server + 'auth/b2AccessRequestToken', { code: code }, { headers: headers })
|
2018-10-05 17:00:54 +02:00
|
|
|
.subscribe((data: any) => {
|
|
|
|
this.authService.login({ ticket: data.payload.accessToken, provider: LoginProviders.B2Accesss, data: null }).subscribe(
|
|
|
|
res => this.onLogInSuccess(res),
|
|
|
|
error => this.onLogInError(error)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NATIVE LOGIN
|
|
|
|
*/
|
|
|
|
|
|
|
|
public nativeLogin(credentials: Credential) {
|
|
|
|
this.authService.nativeLogin(credentials).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,
|
|
|
|
});
|
|
|
|
if (this.authService.current().culture) { this.cultureService.cultureSelected(this.authService.current().culture); }
|
|
|
|
const params = this.router['rawUrlTree'].queryParams;
|
|
|
|
const redirectUrl = params['returnUrl'] ? params['returnUrl'] : '/';
|
|
|
|
this.zone.run(() => this.router.navigate([redirectUrl]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public onLogInError(errorMessage: string) {
|
|
|
|
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
|
|
|
|
data: { message: 'GENERAL.SNACK-BAR.UNSUCCESSFUL-LOGIN', language: this.language },
|
|
|
|
duration: 3000,
|
|
|
|
});
|
|
|
|
}
|
2018-07-11 15:47:36 +02:00
|
|
|
}
|