|
|
|
@ -8,7 +8,10 @@ import { URIKey } from '../../utils/uri-enums';
|
|
|
|
import { StorageService } from '../../shared/services/storage.service';
|
|
|
|
import { StorageService } from '../../shared/services/storage.service';
|
|
|
|
import { I18NService } from '../../services/i18n.service';
|
|
|
|
import { I18NService } from '../../services/i18n.service';
|
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
|
|
|
import { SuccessMessages } from '../../utils/enums';
|
|
|
|
import { FormConstants, SuccessMessages } from '../../utils/enums';
|
|
|
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
|
|
import { AuthenticationService } from '../../services/authenticate.service';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
@Component({
|
|
|
|
selector: 'app-change-password',
|
|
|
|
selector: 'app-change-password',
|
|
|
|
@ -31,7 +34,13 @@ passwordType2: string = 'password';
|
|
|
|
@ViewChild('psh') passwordHideShow?: PasswordHideShowComponent;
|
|
|
|
@ViewChild('psh') passwordHideShow?: PasswordHideShowComponent;
|
|
|
|
@ViewChild('psh1') passwordHideShow1 ?: PasswordHideShowComponent;
|
|
|
|
@ViewChild('psh1') passwordHideShow1 ?: PasswordHideShowComponent;
|
|
|
|
@ViewChild('psh2') passwordHideShow2 ?: PasswordHideShowComponent;
|
|
|
|
@ViewChild('psh2') passwordHideShow2 ?: PasswordHideShowComponent;
|
|
|
|
constructor(private fb: FormBuilder, private httpURIService: HttpURIService, private storageService: StorageService, private i18nService: I18NService){}
|
|
|
|
constructor(
|
|
|
|
|
|
|
|
private fb: FormBuilder,
|
|
|
|
|
|
|
|
private httpURIService: HttpURIService,
|
|
|
|
|
|
|
|
private storageService: StorageService,
|
|
|
|
|
|
|
|
private i18nService: I18NService,
|
|
|
|
|
|
|
|
private router: Router,
|
|
|
|
|
|
|
|
private authService: AuthenticationService ){}
|
|
|
|
|
|
|
|
|
|
|
|
togglePasswordType() {
|
|
|
|
togglePasswordType() {
|
|
|
|
this.passwordType = this.passwordHideShow?.showPassword ? 'password' : 'text';
|
|
|
|
this.passwordType = this.passwordHideShow?.showPassword ? 'password' : 'text';
|
|
|
|
@ -44,7 +53,11 @@ constructor(private fb: FormBuilder, private httpURIService: HttpURIService, pri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.checkIfFirstTimeChangePasswordOrNot();
|
|
|
|
const userStr = this.storageService.getItem('user');
|
|
|
|
|
|
|
|
if (userStr) {
|
|
|
|
|
|
|
|
const user = JSON.parse(userStr);
|
|
|
|
|
|
|
|
this.isFirstLogin = user?.requiresPasswordChange || user?.user?.firstLogin || false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.isFirstLogin) {
|
|
|
|
if (this.isFirstLogin) {
|
|
|
|
this.firstTimeLoginForm = this.fb.group({
|
|
|
|
this.firstTimeLoginForm = this.fb.group({
|
|
|
|
@ -61,7 +74,10 @@ constructor(private fb: FormBuilder, private httpURIService: HttpURIService, pri
|
|
|
|
Validators.maxLength(20),
|
|
|
|
Validators.maxLength(20),
|
|
|
|
Validators.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/)
|
|
|
|
Validators.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/)
|
|
|
|
]]
|
|
|
|
]]
|
|
|
|
}, { validators: this.passwordMatchValidator });
|
|
|
|
}, { validators: [
|
|
|
|
|
|
|
|
this.passwordMatchValidator,
|
|
|
|
|
|
|
|
this.oldAndNewPasswordNotSame,]
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
@ -135,6 +151,11 @@ constructor(private fb: FormBuilder, private httpURIService: HttpURIService, pri
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onSubmit(){
|
|
|
|
onSubmit(){
|
|
|
|
|
|
|
|
if (this.isFirstLogin) {
|
|
|
|
|
|
|
|
this.changeFirstTimePassword();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.changeRegularPassword();
|
|
|
|
|
|
|
|
}
|
|
|
|
const payload = this.getFormPayload();
|
|
|
|
const payload = this.getFormPayload();
|
|
|
|
|
|
|
|
|
|
|
|
this.httpURIService.requestPOST(URIKey.CHANGE_PASSWORD_URI, payload)
|
|
|
|
this.httpURIService.requestPOST(URIKey.CHANGE_PASSWORD_URI, payload)
|
|
|
|
@ -142,6 +163,7 @@ constructor(private fb: FormBuilder, private httpURIService: HttpURIService, pri
|
|
|
|
next: (response) => {
|
|
|
|
next: (response) => {
|
|
|
|
if (!(response instanceof HttpErrorResponse)) {
|
|
|
|
if (!(response instanceof HttpErrorResponse)) {
|
|
|
|
this.i18nService.success(SuccessMessages.CHANGE_PASSWORD_SUCCESS, []);
|
|
|
|
this.i18nService.success(SuccessMessages.CHANGE_PASSWORD_SUCCESS, []);
|
|
|
|
|
|
|
|
this.router.navigate(['/dashboard']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
@ -149,4 +171,83 @@ constructor(private fb: FormBuilder, private httpURIService: HttpURIService, pri
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
changeFirstTimePassword() {
|
|
|
|
|
|
|
|
if (!this.firstTimeLoginForm || this.firstTimeLoginForm.invalid) {
|
|
|
|
|
|
|
|
if (this.firstTimeLoginForm) {
|
|
|
|
|
|
|
|
this.markFormGroupTouched(this.firstTimeLoginForm);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const payload = this.getFormPayload();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.httpURIService.requestPOST(URIKey.CHANGE_PASSWORD_URI, payload)
|
|
|
|
|
|
|
|
.subscribe({
|
|
|
|
|
|
|
|
next: (response) => {
|
|
|
|
|
|
|
|
if (!(response instanceof HttpErrorResponse)) {
|
|
|
|
|
|
|
|
this.i18nService.success(SuccessMessages.CHANGE_PASSWORD_SUCCESS, []);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const userStr = this.storageService.getItem('user');
|
|
|
|
|
|
|
|
if (userStr) {
|
|
|
|
|
|
|
|
const user = JSON.parse(userStr);
|
|
|
|
|
|
|
|
user.requiresPasswordChange = false;
|
|
|
|
|
|
|
|
if (user.user) {
|
|
|
|
|
|
|
|
user.user.firstLogin = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (payload.newPassword) {
|
|
|
|
|
|
|
|
this.storageService.setItem(FormConstants.PASSWORD, payload.newPassword);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.storageService.setItem('user', JSON.stringify(user));
|
|
|
|
|
|
|
|
this.authService.updateCredentialsAfterPasswordChange(payload.newPassword);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.router.navigate(['/home/dashboard']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
error: (error) => {
|
|
|
|
|
|
|
|
console.error('Password change failed:', error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
changeRegularPassword() {
|
|
|
|
|
|
|
|
if (!this.changePasswordForm || this.changePasswordForm.invalid) {
|
|
|
|
|
|
|
|
if (this.changePasswordForm) {
|
|
|
|
|
|
|
|
this.markFormGroupTouched(this.changePasswordForm);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const payload = this.getFormPayload();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.httpURIService.requestPOST(URIKey.CHANGE_PASSWORD_URI, payload)
|
|
|
|
|
|
|
|
.subscribe({
|
|
|
|
|
|
|
|
next: (response) => {
|
|
|
|
|
|
|
|
if (!(response instanceof HttpErrorResponse)) {
|
|
|
|
|
|
|
|
this.i18nService.success(SuccessMessages.CHANGE_PASSWORD_SUCCESS, []);
|
|
|
|
|
|
|
|
this.router.navigate(['/home/dashboard']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
error: (error) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.error('Password change failed:', error);
|
|
|
|
|
|
|
|
if (error.status === 401) {
|
|
|
|
|
|
|
|
this.authService.logout();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private markFormGroupTouched(formGroup: FormGroup) {
|
|
|
|
|
|
|
|
Object.values(formGroup.controls).forEach(control => {
|
|
|
|
|
|
|
|
control.markAsTouched();
|
|
|
|
|
|
|
|
if (control instanceof FormGroup) {
|
|
|
|
|
|
|
|
this.markFormGroupTouched(control);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|