You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
5.2 KiB
TypeScript
111 lines
5.2 KiB
TypeScript
import { HttpErrorResponse } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
import { BehaviorSubject, Observable, Observer } from 'rxjs';
|
|
import { tap } from 'rxjs/operators';
|
|
import { ErrorMessages, FormConstants, HiddenValues, SuccessMessages } from '../utils/enums';
|
|
import { CredentialService } from './credential.service';
|
|
import { AuthenticationToken, UserCredentials } from '../authenticate/authenticate';
|
|
import { HttpURIService } from '../app.http.uri.service';
|
|
import { URIKey } from '../utils/uri-enums';
|
|
import { I18NService } from './i18n.service';
|
|
import { StorageService } from '../shared/services/storage.service';
|
|
import { ButtonManagementService } from './button-management.service';
|
|
|
|
@Injectable(
|
|
{ providedIn: 'root' }
|
|
)
|
|
export class AuthenticationService {
|
|
showLicenseInfo: boolean = false;
|
|
reset: boolean = false;
|
|
|
|
public onAuthenticationComplete: BehaviorSubject<boolean> = new BehaviorSubject(<boolean>false);
|
|
|
|
constructor(private buttonManagementService: ButtonManagementService, private httpService: HttpURIService, private router: Router, private credentialService: CredentialService, private i18nService: I18NService, private storageService: StorageService) {
|
|
}
|
|
|
|
authenticate(uCreds: UserCredentials) : Observable<any> {
|
|
const observable = new Observable((observer: Observer<any>) => {
|
|
|
|
if (this.storageService.getItem('user') != null) {
|
|
this.i18nService.error(ErrorMessages.ALREADY_LOGGED_IN,[]);
|
|
return;
|
|
}
|
|
this.credentialService.setPorOrgacode(uCreds.porOrgacode);
|
|
this.credentialService.setUserId(uCreds.userId);
|
|
this.credentialService.setPassword(uCreds.password);
|
|
this.storageService.setItem(FormConstants.POR_ORGACODE, uCreds.porOrgacode);
|
|
this.storageService.setItem(FormConstants.USER_ID, uCreds.userId);
|
|
this.storageService.setItem(FormConstants.PASSWORD, uCreds.password);
|
|
this.httpService.requestPOST(URIKey.USER_LOGIN_URI, uCreds).subscribe((data: any) => {
|
|
if (!(data instanceof HttpErrorResponse)) {
|
|
data.authenticated = true;
|
|
this.i18nService.success(SuccessMessages.LOGIN_SUCCESSFULLY, []);
|
|
this.storageService.setItem('user', JSON.stringify(data));
|
|
this.credentialService.setToken(data.token);
|
|
this.credentialService.setUserType(data.userType);
|
|
if(data.permission){
|
|
this.storageService.setItem('permission', data.permission);
|
|
this.credentialService.setPermission(JSON.parse(data.permission));
|
|
}
|
|
else{
|
|
this.storageService.setItem('permission', '[]');
|
|
this.credentialService.setPermission([]);
|
|
}
|
|
this.buttonManagementService.setButtonPermissions(this.credentialService.getPermission(), this.isSuperAdminUser());
|
|
if(data.user.isFirstLogin){
|
|
this.router.navigate(["/changepassword"]);
|
|
} else {
|
|
this.router.navigate(["/home/dashboard"]);
|
|
}
|
|
this.onAuthenticationComplete.next(true);
|
|
observer.complete();
|
|
}
|
|
else {
|
|
this.onAuthenticationComplete.next(false);
|
|
observer.error(false);
|
|
}
|
|
});
|
|
});
|
|
return observable;
|
|
|
|
}
|
|
|
|
isAuthenticated(): boolean {
|
|
if (this.storageService && this.storageService.getItem('user') != null) {
|
|
let cachedUser = JSON.parse(this.storageService.getItem('user') || '{}');
|
|
return cachedUser.authenticated;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
isSuperAdminUser(){
|
|
if (this.storageService && this.storageService.getItem('user') != null) {
|
|
let cachedUser = JSON.parse(this.storageService.getItem('user') || '{}');
|
|
return cachedUser.userType === HiddenValues.SUPERADMIN_USER;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
refreshToken() {
|
|
let uCreds: UserCredentials = { porOrgacode: this.credentialService.getPorOrgacode(), userId: this.credentialService.getUserId(), password: this.credentialService.getPassword(), token: this.credentialService.getToken() };
|
|
return this.httpService.requestPOST<AuthenticationToken>(URIKey.USER_REFRESH_TOKEN, uCreds).pipe(
|
|
tap(response => {
|
|
this.credentialService.setToken(response.token);
|
|
let cachedUser = JSON.parse(this.storageService.getItem('user') || '{}');
|
|
cachedUser.token = response.token;
|
|
this.storageService.setItem('user', JSON.stringify(cachedUser));
|
|
})
|
|
);
|
|
}
|
|
|
|
logout() {
|
|
let defaultPermission: string = this.storageService.getItem("defaultPermission") || "{}";
|
|
this.storageService.clear();
|
|
this.storageService.setItem("defaultPermission", defaultPermission)
|
|
this.credentialService.resetService();
|
|
this.router.navigate(['/login']);
|
|
|
|
}
|
|
|
|
}
|