2019-09-23 10:17:03 +02:00
|
|
|
|
2018-11-27 15:13:56 +01:00
|
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
2017-12-14 11:41:26 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-09-23 10:17:03 +02:00
|
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
2017-12-14 11:41:26 +01:00
|
|
|
import { Router } from '@angular/router';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { Credential } from '@app/core/model/auth/credential';
|
|
|
|
import { LoginInfo } from '@app/core/model/auth/login-info';
|
2020-04-27 15:21:03 +02:00
|
|
|
import { Principal } from '@app/core/model/auth/principal';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { ConfigurableProvider } from '@app/core/model/configurable-provider/configurableProvider';
|
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
|
|
|
|
import { BaseService } from '@common/base/base.service';
|
2018-11-27 15:13:56 +01:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { environment } from 'environments/environment';
|
|
|
|
import { Observable, of as observableOf, throwError as observableThrowError } from 'rxjs';
|
|
|
|
import { catchError, map, takeUntil } from 'rxjs/operators';
|
2020-03-26 17:44:12 +01:00
|
|
|
import { ConfigurationService } from '../configuration/configuration.service';
|
2020-06-25 12:35:17 +02:00
|
|
|
import { CookieService } from 'ngx-cookie-service';
|
2017-12-14 11:41:26 +01:00
|
|
|
|
|
|
|
@Injectable()
|
2018-11-27 18:33:17 +01:00
|
|
|
export class AuthService extends BaseService {
|
2018-10-05 17:00:54 +02:00
|
|
|
private actionUrl: string;
|
|
|
|
private headers: HttpHeaders;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private http: HttpClient,
|
2019-02-15 10:18:14 +01:00
|
|
|
private snackBar: MatSnackBar,
|
|
|
|
private language: TranslateService,
|
|
|
|
private router: Router,
|
2020-03-26 17:44:12 +01:00
|
|
|
private uiNotificationService: UiNotificationService,
|
2020-06-25 12:35:17 +02:00
|
|
|
private configurationService: ConfigurationService,
|
|
|
|
private cookieService: CookieService
|
2018-10-05 17:00:54 +02:00
|
|
|
) {
|
2018-11-27 18:33:17 +01:00
|
|
|
super();
|
2020-03-26 17:44:12 +01:00
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
|
|
|
|
this.headers = new HttpHeaders();
|
|
|
|
this.headers = this.headers.set('Content-Type', 'application/json');
|
|
|
|
this.headers = this.headers.set('Accept', 'application/json');
|
|
|
|
}
|
|
|
|
|
|
|
|
private clear(): void {
|
|
|
|
localStorage.removeItem('principal');
|
|
|
|
}
|
|
|
|
|
|
|
|
public current(principal?: Principal): Principal {
|
|
|
|
if (principal) {
|
|
|
|
localStorage.setItem('principal', JSON.stringify(principal));
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
const principalJson = localStorage.getItem('principal');
|
2020-09-14 17:09:57 +02:00
|
|
|
if (principalJson === null || principalJson === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-07 17:14:54 +02:00
|
|
|
let principalObj = JSON.parse(principalJson) as Principal;
|
|
|
|
principalObj.expiresAt = new Date(principalObj.expiresAt);
|
|
|
|
if (principalObj.expiresAt < new Date()) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-05 17:00:54 +02:00
|
|
|
return principalObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
//public login(credential: Credential): Observable<Principal> {
|
|
|
|
// const url = this.actionUrl + 'login';
|
|
|
|
|
|
|
|
// return this.http.post(url, credential, { headers: this.headers })
|
|
|
|
// .map((res: Response) => {
|
|
|
|
// let principal = this.current(new JsonSerializer<Principal>().fromJSONObject(res, Principal));
|
|
|
|
// //this.loginContextSubject.next(true);
|
|
|
|
// return principal;
|
|
|
|
// })
|
|
|
|
// .catch((error: any) => {
|
|
|
|
// //this.loginContextSubject.next(false);
|
|
|
|
// return Observable.throw(error);
|
|
|
|
// });
|
|
|
|
//}
|
|
|
|
|
|
|
|
public login(loginInfo: LoginInfo): Observable<Principal> {
|
2020-03-26 17:44:12 +01:00
|
|
|
this.actionUrl = this.configurationService.server + 'auth/';
|
2018-10-05 17:00:54 +02:00
|
|
|
const url = this.actionUrl + 'externallogin';
|
|
|
|
|
2019-09-23 10:17:03 +02:00
|
|
|
return this.http.post(url, loginInfo, { headers: this.headers }).pipe(
|
|
|
|
map((res: any) => {
|
2019-01-18 18:03:45 +01:00
|
|
|
const principal = this.current(res.payload);
|
2021-03-18 13:48:58 +01:00
|
|
|
// this.cookieService.set('cookiesConsent', 'true', 356);
|
|
|
|
this.cookieService.set("cookiesConsent", "true", 356,null,null,false, 'Lax');
|
2018-10-05 17:00:54 +02:00
|
|
|
//this.loginContextSubject.next(true);
|
|
|
|
return principal;
|
2019-09-23 10:17:03 +02:00
|
|
|
}),
|
|
|
|
catchError((error: any) => {
|
2018-10-05 17:00:54 +02:00
|
|
|
//this.loginContextSubject.next(false);
|
2019-09-23 10:17:03 +02:00
|
|
|
return observableThrowError(error);
|
2019-11-13 16:32:55 +01:00
|
|
|
}));
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2020-10-26 17:18:26 +01:00
|
|
|
public mergeLogin(loginInfo: LoginInfo): Observable<any> {
|
|
|
|
this.actionUrl = this.configurationService.server + 'auth/';
|
|
|
|
const url = this.actionUrl + 'externallogin';
|
|
|
|
|
|
|
|
return this.http.post(url, loginInfo, { headers: this.headers });
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
public nativeLogin(credentials: Credential): Observable<Principal> {
|
2020-03-26 17:44:12 +01:00
|
|
|
this.actionUrl = this.configurationService.server + 'auth/';
|
2018-10-05 17:00:54 +02:00
|
|
|
const url = this.actionUrl + 'nativelogin';
|
|
|
|
|
2019-09-23 10:17:03 +02:00
|
|
|
return this.http.post(url, credentials, { headers: this.headers }).pipe(
|
|
|
|
map((res: any) => {
|
2019-01-18 18:03:45 +01:00
|
|
|
const principal = this.current(res.payload);
|
2021-03-18 13:48:58 +01:00
|
|
|
// this.cookieService.set('cookiesConsent', 'true', 356);
|
|
|
|
this.cookieService.set("cookiesConsent", "true", 356,null,null,false, 'Lax');
|
2018-10-05 17:00:54 +02:00
|
|
|
//this.loginContextSubject.next(true);
|
|
|
|
return principal;
|
2019-09-23 10:17:03 +02:00
|
|
|
}),
|
|
|
|
catchError((error: any) => {
|
2018-10-05 17:00:54 +02:00
|
|
|
//this.loginContextSubject.next(false);
|
2019-09-23 10:17:03 +02:00
|
|
|
return observableThrowError(error);
|
2019-11-13 16:32:55 +01:00
|
|
|
}));
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public logout(): void {
|
2020-03-26 17:44:12 +01:00
|
|
|
this.actionUrl = this.configurationService.server + 'auth/';
|
2018-10-05 17:00:54 +02:00
|
|
|
const url = this.actionUrl + 'logout';
|
|
|
|
const principal = this.current();
|
|
|
|
this.clear();
|
|
|
|
|
|
|
|
if (!principal) { return; }
|
|
|
|
let headers = this.headers;
|
|
|
|
headers = headers.set('AuthToken', principal.token);
|
2018-11-27 18:33:17 +01:00
|
|
|
this.http.post(url, null, { headers: headers })
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
res => this.onLogOutSuccess(res),
|
|
|
|
error => this.onLogOutError(error)
|
|
|
|
);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public me(): Observable<Principal> {
|
2020-09-15 12:16:18 +02:00
|
|
|
this.actionUrl = this.configurationService.server + 'auth/';
|
|
|
|
const url = this.actionUrl + 'me';
|
2018-10-05 17:00:54 +02:00
|
|
|
const principal = this.current();
|
|
|
|
if (!principal) {
|
|
|
|
this.clear();
|
2019-09-23 10:17:03 +02:00
|
|
|
return observableOf<Principal>();
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
let headers = this.headers;
|
|
|
|
headers = headers.set('AuthToken', principal.token);
|
2019-09-23 10:17:03 +02:00
|
|
|
return this.http.post(url, null, { headers: headers }).pipe(
|
|
|
|
map((res: any) => {
|
2019-01-18 18:03:45 +01:00
|
|
|
const princ = this.current(res.payload);
|
2020-09-07 17:14:54 +02:00
|
|
|
princ.expiresAt = new Date(princ.expiresAt);
|
|
|
|
console.log("Token Expires at: " + princ.expiresAt.toDateString() + ' ' + princ.expiresAt.toLocaleTimeString());
|
2018-10-05 17:00:54 +02:00
|
|
|
return princ;
|
2019-09-23 10:17:03 +02:00
|
|
|
}),
|
|
|
|
catchError((error: any) => {
|
2018-10-08 16:58:52 +02:00
|
|
|
//console.warn('could not retrieve me info:\n', error);
|
2018-10-05 17:00:54 +02:00
|
|
|
this.clear();
|
|
|
|
const princ = this.current();
|
|
|
|
this.router.navigate(['/login']);
|
2019-09-23 10:17:03 +02:00
|
|
|
return observableOf<Principal>(princ);
|
2019-11-13 16:32:55 +01:00
|
|
|
}));
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public onLogOutSuccess(logoutMessage: any) {
|
2019-02-15 10:18:14 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-LOGOUT'), SnackBarNotificationLevel.Success);
|
2018-10-05 17:00:54 +02:00
|
|
|
this.router.navigate(['/login']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public onLogOutError(errorMessage: string) {
|
2019-02-15 10:18:14 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-LOGOUT'), SnackBarNotificationLevel.Error);
|
2018-10-05 17:00:54 +02:00
|
|
|
this.router.navigate(['/login']);
|
|
|
|
}
|
2019-11-13 16:32:55 +01:00
|
|
|
|
|
|
|
public getConfigurableProviders(): Observable<ConfigurableProvider[]> {
|
2020-03-27 09:51:49 +01:00
|
|
|
this.actionUrl = this.configurationService.server + 'auth/';
|
2019-11-13 16:32:55 +01:00
|
|
|
const url = this.actionUrl + 'configurableLogin';
|
|
|
|
return this.http.get<ConfigurableProvider[]>(url, { headers: this.headers }).pipe(
|
|
|
|
map((res: any) => {
|
|
|
|
const providers = res.payload.providers;
|
|
|
|
return providers;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2017-12-14 11:41:26 +01:00
|
|
|
}
|