This repository has been archived on 2025-06-18. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
casino/frontend/src/app/feature/auth/recover-password/recover-password.component.ts
csimonis 9a95ad3d0f
Some checks failed
CI / Get Changed Files (pull_request) Successful in 8s
CI / Checkstyle Main (pull_request) Has been skipped
CI / Docker backend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Successful in 19s
CI / eslint (pull_request) Successful in 28s
CI / prettier (pull_request) Failing after 26s
CI / test-build (pull_request) Successful in 42s
CI / Docker frontend validation (pull_request) Successful in 48s
feat(auth): add recover password functionality and forms
2025-05-15 12:53:53 +02:00

134 lines
3.7 KiB
TypeScript

import { Component, EventEmitter, Output, signal, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { AuthService } from '@service/auth.service';
@Component({
selector: 'app-recover-password',
standalone: true,
imports: [CommonModule, ReactiveFormsModule, RouterModule],
templateUrl: './recover-password.component.html',
})
export class RecoverPasswordComponent implements OnInit {
emailForm: FormGroup;
resetPasswordForm: FormGroup;
errorMessage = signal('');
successMessage = signal('');
isLoading = signal(false);
token = '';
isResetMode = signal(false);
@Output() closeDialog = new EventEmitter<void>();
@Output() switchToLogin = new EventEmitter<void>();
constructor(
private fb: FormBuilder,
private authService: AuthService,
private router: Router,
private route: ActivatedRoute
) {
this.emailForm = this.fb.group({
email: ['', [Validators.required, Validators.email]],
});
this.resetPasswordForm = this.fb.group(
{
password: ['', [Validators.required, Validators.minLength(8)]],
confirmPassword: ['', [Validators.required]],
},
{
validators: this.passwordMatchValidator,
}
);
}
ngOnInit(): void {
// Check if we're in reset mode via URL parameters
// This is still needed for direct access via URLs with token
this.route.queryParamMap.subscribe((params) => {
const token = params.get('token');
if (token) {
this.token = token;
this.isResetMode.set(true);
}
});
}
passwordMatchValidator(form: FormGroup) {
const password = form.get('password')?.value;
const confirmPassword = form.get('confirmPassword')?.value;
return password === confirmPassword ? null : { passwordMismatch: true };
}
get emailFormControls() {
return this.emailForm.controls;
}
get resetFormControls() {
return this.resetPasswordForm.controls;
}
onSubmitEmail(): void {
if (this.emailForm.invalid) {
return;
}
this.isLoading.set(true);
this.errorMessage.set('');
this.successMessage.set('');
const email = this.emailFormControls['email'].value;
this.authService.recoverPassword(email).subscribe({
next: () => {
this.isLoading.set(false);
this.successMessage.set(
'Wenn ein Konto mit dieser E-Mail existiert, wird eine E-Mail mit weiteren Anweisungen gesendet.'
);
this.emailForm.reset();
},
error: (err) => {
this.isLoading.set(false);
this.errorMessage.set(
err.error?.message || 'Ein Fehler ist aufgetreten. Bitte versuche es später erneut.'
);
},
});
}
onSubmitReset(): void {
if (this.resetPasswordForm.invalid) {
return;
}
this.isLoading.set(true);
this.errorMessage.set('');
this.successMessage.set('');
const password = this.resetFormControls['password'].value;
this.authService.resetPassword(this.token, password).subscribe({
next: () => {
this.isLoading.set(false);
this.successMessage.set(
'Dein Passwort wurde erfolgreich zurückgesetzt. Du kannst dich jetzt anmelden.'
);
setTimeout(() => {
this.closeDialog.emit();
this.switchToLogin.emit();
}, 3000);
},
error: (err) => {
this.isLoading.set(false);
this.errorMessage.set(
err.error?.message || 'Ein Fehler ist aufgetreten. Bitte versuche es später erneut.'
);
},
});
}
goBackToLogin(): void {
this.switchToLogin.emit();
}
}