Files
smartguide/webapp/src/app/modules/reset-password/reset-password.component.ts
T

167 lines
4.7 KiB
TypeScript

import { Component, DestroyRef, inject, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import {
AbstractControl,
FormControl,
FormGroup,
ReactiveFormsModule,
ValidationErrors,
Validators,
} from '@angular/forms';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { HttpErrorResponse } from '@angular/common/http';
import { finalize } from 'rxjs';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { CredentialService } from '../../shared/services/credential.service';
import {
CHECK_CIRCLE_ICON,
ERROR_OUTLINE_ICON,
VISIBILITY_ICON,
VISIBILITY_OFF_ICON,
} from '../../shared/ui/icons';
@Component({
selector: 'app-reset-password',
standalone: true,
imports: [
ReactiveFormsModule,
RouterLink,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
MatIconModule,
MatSnackBarModule,
],
templateUrl: './reset-password.component.html',
styleUrl: '../app-login/app-login.component.scss',
})
export class ResetPasswordComponent {
readonly CHECK_CIRCLE_ICON = CHECK_CIRCLE_ICON;
readonly ERROR_OUTLINE_ICON = ERROR_OUTLINE_ICON;
readonly VISIBILITY_ICON = VISIBILITY_ICON;
readonly VISIBILITY_OFF_ICON = VISIBILITY_OFF_ICON;
private readonly destroyRef = inject(DestroyRef);
private readonly snackBar = inject(MatSnackBar);
private readonly resetToken = signal<string | undefined>(undefined);
readonly isSubmitting = signal(false);
readonly passwordChanged = signal(false);
readonly showPasswords = signal(false);
readonly linkInvalid = signal(false);
readonly errorMessage = signal<string | undefined>(undefined);
readonly form = new FormGroup({
newPassword: new FormControl<string>('', [
Validators.required,
Validators.minLength(8),
Validators.maxLength(64),
]),
confirmPassword: new FormControl<string>('', [Validators.required]),
});
constructor(
private readonly credentialService: CredentialService,
route: ActivatedRoute,
) {
const token = route.snapshot.queryParamMap.get('token') ?? undefined;
this.resetToken.set(token);
this.form.controls.confirmPassword.addValidators(
this.passwordsMatchValidator.bind(this),
);
this.form.controls.newPassword.valueChanges
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => {
this.form.controls.confirmPassword.updateValueAndValidity();
});
}
hasToken(): boolean {
return !!this.resetToken();
}
resetPassword(): void {
if (!this.hasToken()) {
this.linkInvalid.set(true);
this.errorMessage.set(
'Der Link zum Zurücksetzen des Passworts ist ungültig oder unvollständig.',
);
return;
}
if (this.form.invalid || this.isSubmitting()) {
this.form.markAllAsTouched();
return;
}
const resetToken = this.resetToken();
const newPassword = this.form.controls.newPassword.value ?? '';
if (!resetToken) {
this.linkInvalid.set(true);
return;
}
this.isSubmitting.set(true);
this.errorMessage.set(undefined);
this.form.disable();
this.credentialService
.resetPassword(resetToken, newPassword)
.pipe(
finalize(() => {
this.isSubmitting.set(false);
}),
)
.subscribe({
next: () => {
this.passwordChanged.set(true);
},
error: (error: HttpErrorResponse) => {
console.error('Password reset failed', error);
if (
error.status === 400 ||
error.status === 401 ||
error.status === 403 ||
error.status === 404
) {
this.linkInvalid.set(true);
this.snackBar.open(
'Der Link zum Zurücksetzen des Passworts ist abgelaufen oder wurde bereits verwendet.',
'OK',
{ duration: 7000 },
);
return;
}
this.errorMessage.set(
'Das Passwort konnte nicht geändert werden. Bitte versuche es später erneut.',
);
this.form.enable();
},
});
}
private passwordsMatchValidator(
control: AbstractControl,
): ValidationErrors | null {
const newPassword = this.form.controls.newPassword.value;
const confirmPassword = control.value;
if (!newPassword || !confirmPassword) {
return null;
}
return newPassword === confirmPassword ? null : { passwordsMismatch: true };
}
}