2017-12-14 11:41:26 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpHeaders } from '@angular/common/http';
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
import { HostConfiguration } from '../../app.constants';
|
|
|
|
import { Principal } from '../../models/login/Principal';
|
|
|
|
import { Credential } from '../../models/login/Credential';
|
|
|
|
import { Observable } from 'rxjs/Rx';
|
|
|
|
import { MatSnackBar } from '@angular/material';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
import { JsonSerializer } from '../../utilities/JsonSerializer';
|
|
|
|
import { LoginInfo } from '../../models/login/LoginInfo';
|
|
|
|
import { SnackBarNotificationComponent } from '../../shared/components/notificaiton/snack-bar-notification.component';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AuthService {
|
|
|
|
private actionUrl: string;
|
|
|
|
private headers: HttpHeaders;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private http: HttpClient,
|
|
|
|
public snackBar: MatSnackBar,
|
|
|
|
public language: TranslateService,
|
|
|
|
public router: Router
|
|
|
|
) {
|
|
|
|
|
2017-12-19 10:02:25 +01:00
|
|
|
this.actionUrl = HostConfiguration.Server + 'auth/';
|
2017-12-14 11:41:26 +01: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');
|
|
|
|
if (!principalJson) { return null; }
|
|
|
|
const principalObj = JSON.parse(principalJson) as Principal;
|
|
|
|
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> {
|
2017-12-19 09:38:47 +01:00
|
|
|
const url = this.actionUrl + 'externallogin';
|
2017-12-14 11:41:26 +01:00
|
|
|
|
|
|
|
return this.http.post(url, loginInfo, { headers: this.headers })
|
2017-12-19 09:38:47 +01:00
|
|
|
.map((res: any) => {
|
2018-01-03 17:36:31 +01:00
|
|
|
const principal = this.current(JsonSerializer.fromJSONObject(res.payload, Principal));
|
2017-12-14 11:41:26 +01:00
|
|
|
//this.loginContextSubject.next(true);
|
|
|
|
return principal;
|
|
|
|
})
|
|
|
|
.catch((error: any) => {
|
|
|
|
//this.loginContextSubject.next(false);
|
|
|
|
return Observable.throw(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public logout(): void {
|
|
|
|
const url = this.actionUrl + 'logout';
|
|
|
|
const principal = this.current();
|
|
|
|
this.clear();
|
|
|
|
|
|
|
|
if (!principal) { return; }
|
|
|
|
let headers = this.headers;
|
|
|
|
headers = headers.set('AuthToken', principal.token)
|
|
|
|
this.http.post(url, null, { headers: headers }).subscribe(
|
|
|
|
res => this.onLogOutSuccess(res),
|
|
|
|
error => this.onLogOutError(error)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
public me(): Observable<Principal> {
|
|
|
|
const url = this.actionUrl + '/me';
|
|
|
|
const principal = this.current();
|
|
|
|
if (!principal) {
|
|
|
|
this.clear();
|
|
|
|
return Observable.of<Principal>();
|
|
|
|
}
|
|
|
|
let headers = this.headers;
|
|
|
|
headers = headers.set('AuthToken', principal.token)
|
|
|
|
return this.http.post(url, null, { headers: headers })
|
2018-01-31 16:39:16 +01:00
|
|
|
.map((res: any) => {
|
|
|
|
const princ = this.current(JsonSerializer.fromJSONObject(res.payload, Principal));
|
2017-12-14 11:41:26 +01:00
|
|
|
return princ;
|
|
|
|
})
|
|
|
|
.catch((error: any) => {
|
|
|
|
console.warn('could not retrieve me info:\n', error);
|
|
|
|
this.clear();
|
|
|
|
const princ = this.current();
|
|
|
|
this.router.navigate(['/login']);
|
|
|
|
return Observable.of<Principal>(princ);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public onLogOutSuccess(logoutMessage: any) {
|
|
|
|
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
|
|
|
|
data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-LOGOUT', language: this.language },
|
|
|
|
duration: 3000,
|
|
|
|
extraClasses: ['snackbar-success']
|
|
|
|
})
|
|
|
|
this.router.navigate(['/login']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public onLogOutError(errorMessage: string) {
|
|
|
|
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
|
|
|
|
data: { message: 'GENERAL.SNACK-BAR.UNSUCCESSFUL-LOGOUT', language: this.language },
|
|
|
|
duration: 3000,
|
|
|
|
extraClasses: ['snackbar-warning']
|
|
|
|
})
|
|
|
|
this.router.navigate(['/login']);
|
|
|
|
}
|
|
|
|
}
|