refactor: throw proper error on registration conflict, handle properly
This commit is contained in:
parent
46e52e20cc
commit
b4351ceaea
4 changed files with 93 additions and 45 deletions
|
@ -1,9 +1,10 @@
|
|||
import { Component } from '@angular/core';
|
||||
import { Component, signal } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
|
||||
import { Router, RouterLink } from '@angular/router';
|
||||
import { RegisterRequest } from '../../../model/auth/RegisterRequest';
|
||||
import { AuthService } from '@service/auth.service';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { HttpErrorResponse } from '@angular/common/http';
|
||||
|
||||
@Component({
|
||||
selector: 'app-register',
|
||||
|
@ -13,8 +14,9 @@ import { CommonModule } from '@angular/common';
|
|||
})
|
||||
export class RegisterComponent {
|
||||
registerForm: FormGroup;
|
||||
errorMessage = '';
|
||||
isLoading = false;
|
||||
errorMessage = signal<string>('');
|
||||
isLoading = signal<boolean>(false);
|
||||
fieldErrors = signal<{ [key: string]: string }>({});
|
||||
|
||||
constructor(
|
||||
private fb: FormBuilder,
|
||||
|
@ -37,8 +39,9 @@ export class RegisterComponent {
|
|||
return;
|
||||
}
|
||||
|
||||
this.isLoading = true;
|
||||
this.errorMessage = '';
|
||||
this.isLoading.set(true);
|
||||
this.errorMessage.set('');
|
||||
this.fieldErrors.set({});
|
||||
|
||||
const registerRequest: RegisterRequest = {
|
||||
email: this.form['email'].value,
|
||||
|
@ -59,15 +62,33 @@ export class RegisterComponent {
|
|||
this.router.navigate(['/home']);
|
||||
},
|
||||
error: () => {
|
||||
this.isLoading = false;
|
||||
this.errorMessage =
|
||||
'Registration successful but failed to login automatically. Please log in manually.';
|
||||
this.isLoading.set(false);
|
||||
this.errorMessage.set(
|
||||
'Registration successful but failed to login automatically. Please log in manually.'
|
||||
);
|
||||
},
|
||||
});
|
||||
},
|
||||
error: (err) => {
|
||||
this.isLoading = false;
|
||||
this.errorMessage = err.error?.message || 'Failed to register. Please try again.';
|
||||
error: (err: HttpErrorResponse) => {
|
||||
this.isLoading.set(false);
|
||||
|
||||
if (err.status === 409) {
|
||||
const errorResponse = err.error;
|
||||
|
||||
if (errorResponse?.message === 'Email is already in use') {
|
||||
this.fieldErrors.update((errors) => ({
|
||||
...errors,
|
||||
email: 'Diese E-Mail-Adresse wird bereits verwendet.',
|
||||
}));
|
||||
} else if (errorResponse?.message === 'Username is already taken') {
|
||||
this.fieldErrors.update((errors) => ({
|
||||
...errors,
|
||||
username: 'Dieser Benutzername ist bereits vergeben.',
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
this.errorMessage.set(err.error?.message || 'Failed to register. Please try again.');
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
Reference in a new issue