Untitled
unknown
plain_text
10 months ago
8.2 kB
8
Indexable
import {
Component,
EventEmitter,
Injector,
Input,
OnDestroy,
OnInit,
Output,
} from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { ErrorComponent } from '@ziwig/ng-core';
import { IErrorSignal, IErrorSub } from '@ziwig/types';
import { AuthenticationService } from '../../authentication.service';
import { SignalKeysAuthenticationService } from '../../authentication.enum';
import { LayoutToastsService } from '@ziwig/ng-layout';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { TokenStorageService } from 'projects/libs/_shared/src/lib/services/token-storage.service';
import { KeycloakLoginFlowService } from '../../keycloak-login-flow.service';
@Component({
selector: 'zg-app-reset-password-controller',
template: `
<zg-app-edit-password
[data]="data"
(EditPasswordevent)="editPassword($event)"
(credentialsTokenEmitter)="handleToken($event)"
>
</zg-app-edit-password>
`,
})
export class EditNewPasswordControllerComponent
extends ErrorComponent
implements OnInit, OnDestroy
{
private $authRefService!: Subscription;
token: any;
@Input() SessionIdData!: any;
@Output() EditPasswordevent: EventEmitter<any> = new EventEmitter<any>();
accessToken!: string | null;
data!: any;
componentMode!: string;
selectedDomain!: any;
sessionId!: string | null;
lang: string = 'en';
private tokenSubject = new Subject<string | null>();
constructor(
private authenticationService: AuthenticationService,
private layoutToastService: LayoutToastsService,
injector: Injector,
private route: Router,
private translate: TranslateService,
private activatedRoute: ActivatedRoute,
private tokenStorageService: TokenStorageService,
private KeycloakLoginFlowService: KeycloakLoginFlowService
) {
super(injector);
this.translate.onLangChange.subscribe(() => {
this.lang = this.translate.currentLang;
});
}
ngOnInit(): void {
this.listenToAuthEvents();
this.selectedDomain = this.activatedRoute.snapshot.paramMap.get('domain');
this.sessionId = this.activatedRoute.snapshot.paramMap.get('sessionId');
this.componentMode = this.activatedRoute.snapshot.data['operation'];
this.getPasswordPolicy();
this.tokenSubject.subscribe((token) => {
console.log('Received token:', token);
console.log('Selected domain (realm):', this.selectedDomain);
if (token) {
this.authenticationService.checkToken(this.selectedDomain, token);
} else {
console.log('Token is not available.');
}
});
}
handleToken(event: any) {
this.token = event;
this.tokenSubject.next(this.token);
}
getPasswordPolicy() {
this.authenticationService.getPasswordPolicy(this.selectedDomain).subscribe(
(data: any) => {
const minLengthMatch = /length\((\d+)\)/.exec(data);
const maxLengthMatch = /maxLength\((\d+)\)/.exec(data);
const lowerCaseMatch = /lowerCase\((\d+)\)/.exec(data);
const upperCaseMatch = /upperCase\((\d+)\)/.exec(data);
const specialCharsMatch = /specialChars\((\d+)\)/.exec(data);
this.data = {
minLength: minLengthMatch ? parseInt(minLengthMatch[1], 10) : null,
maxLength: maxLengthMatch ? parseInt(maxLengthMatch[1], 10) : null,
lowerCaseCount: lowerCaseMatch
? parseInt(lowerCaseMatch[1], 10)
: null,
upperCaseCount: upperCaseMatch
? parseInt(upperCaseMatch[1], 10)
: null,
specialCharsCount: specialCharsMatch
? parseInt(specialCharsMatch[1], 10)
: null,
};
},
(error: any) => {
console.error('Error fetching password policy:', error);
}
);
}
// edit password for the Forgot password flow or for the update password required action flow.
editPassword(event: any) {
if (this.selectedDomain) {
if (this.componentMode && this.componentMode === 'required-action') {
const cguRequiredAction = this.tokenStorageService.getRequiredAction();
if (
cguRequiredAction.execution &&
cguRequiredAction.formAction &&
cguRequiredAction.cookie &&
cguRequiredAction.username
) {
this.authenticationService.RequiredAction(
{
newPassword: event.newPassword,
confirmPassword: event.newPassword,
execution: cguRequiredAction.execution,
formAction: cguRequiredAction.formAction,
cookie: cguRequiredAction.cookie,
username: cguRequiredAction.username,
},
this.selectedDomain
);
}
} else {
let params: any = {
userSessionId: this.sessionId as string,
newPassword: event.newPassword,
};
if (this.componentMode && this.componentMode === 'init-password') {
params = {
...params,
initPassword: true,
lang: this.lang,
token: event.token,
};
}
this.authenticationService.ResetPassword(params, this.selectedDomain);
}
}
}
private listenToAuthEvents() {
this.$authRefService = this.authenticationService
.getUpdateListener([
SignalKeysAuthenticationService.RESET_PASSWORD,
SignalKeysAuthenticationService.REQUIRED_ACTION,
SignalKeysAuthenticationService.CHECK_TOKEN_PASSWORD,
])
.subscribe((dataEvent: any) => {
const { key, data } = dataEvent;
switch (key) {
// case SignalKeysAuthenticationService.CHECK_TOKEN_PASSWORD: {
// if (data.check === 'true') {
// const successMessage = this.translate.instant(
// 'Your session is still active.'
// );
// this.layoutToastService.showSuccess(successMessage);
// }
// // else {
// // const errorMessage = this.translate.instant(
// // 'Your session has expired. Please start the process from the beginning.'
// // );
// // this.layoutToastService.showError(errorMessage);
// // this.route.navigate([
// // `/${this.selectedDomain}/auth/update-credential/verify-user`,
// // ]);
// // }
// break;
// }
case SignalKeysAuthenticationService.RESET_PASSWORD: {
if (data.message === 'password updated successfully') {
const successMessage = this.translate.instant(
'Password updated successfully.'
);
this.layoutToastService.showSuccess(successMessage);
this.route.navigate([`/${this.selectedDomain}/auth/login-sat`]);
} else {
const errorMessage = this.translate.instant(
'Your session has expired. Please start the process from the beginning.'
);
this.layoutToastService.showError(errorMessage);
this.route.navigate([
`/${this.selectedDomain}/auth/update-credential/verify-user`,
]);
}
break;
}
case SignalKeysAuthenticationService.REQUIRED_ACTION: {
if (this.selectedDomain) {
this.KeycloakLoginFlowService.getAuthFlow(
data,
this.selectedDomain
);
}
break;
}
}
});
}
ngOnDestroy(): void {
this.$authRefService.unsubscribe();
}
protected gettingError = (errData: IErrorSignal<IErrorSub, string>) => {
const { err } = errData;
this.data = {
errStatus: err.err.status,
message_error: err.err?.error?.message,
};
};
}
Editor is loading...
Leave a Comment