fix: adjust handling of backend errors
This commit is contained in:
parent
0646aaae6b
commit
c2c5bd850e
4 changed files with 31 additions and 4 deletions
|
|
@ -79,7 +79,6 @@ export class LoginComponent {
|
||||||
this.loginData.email = this.loginData.email.toLowerCase();
|
this.loginData.email = this.loginData.email.toLowerCase();
|
||||||
try {
|
try {
|
||||||
await this.authService.login(this.loginData, this.checkboxRememberMe);
|
await this.authService.login(this.loginData, this.checkboxRememberMe);
|
||||||
this.router.navigate(['/summary']);
|
|
||||||
this.buttonStateService.enableButton();
|
this.buttonStateService.enableButton();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.buttonStateService.enableButton();
|
this.buttonStateService.enableButton();
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,11 @@ export class AuthInterceptor implements HttpInterceptor {
|
||||||
|
|
||||||
return next.handle(clonedReq).pipe(
|
return next.handle(clonedReq).pipe(
|
||||||
catchError((error: HttpErrorResponse) => {
|
catchError((error: HttpErrorResponse) => {
|
||||||
|
if (error.status === 0 && !this.isNavigatingToLogin) {
|
||||||
|
this.tokenService.deleteAuthToken();
|
||||||
|
this.tokenService.deleteUserId();
|
||||||
|
this.router.navigate(['/login']);
|
||||||
|
}
|
||||||
if (error.status === 401 && !this.isNavigatingToLogin) {
|
if (error.status === 401 && !this.isNavigatingToLogin) {
|
||||||
this.toast.showSessionExpiredToast();
|
this.toast.showSessionExpiredToast();
|
||||||
this.router.navigate(['/login']);
|
this.router.navigate(['/login']);
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ export class AuthService {
|
||||||
this.tokenService.storeUserId(userId, storage);
|
this.tokenService.storeUserId(userId, storage);
|
||||||
this.currentUserIdSubject.next(userId);
|
this.currentUserIdSubject.next(userId);
|
||||||
this.toastNotificationService.loginSuccessToast();
|
this.toastNotificationService.loginSuccessToast();
|
||||||
|
this.router.navigate(['/summary']);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed:', error);
|
console.error('Login failed:', error);
|
||||||
}
|
}
|
||||||
|
|
@ -65,8 +66,8 @@ export class AuthService {
|
||||||
await lastValueFrom(
|
await lastValueFrom(
|
||||||
this.apiService.request('POST', `/auth/register/`, credentials)
|
this.apiService.request('POST', `/auth/register/`, credentials)
|
||||||
);
|
);
|
||||||
this.router.navigate(['/login']);
|
|
||||||
this.toastNotificationService.registerSuccessToast();
|
this.toastNotificationService.registerSuccessToast();
|
||||||
|
this.router.navigate(['/login']);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Registration failed:', error);
|
console.error('Registration failed:', error);
|
||||||
throw new Error(error?.error?.error);
|
throw new Error(error?.error?.error);
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,13 @@ import { ToastrService } from 'ngx-toastr';
|
||||||
})
|
})
|
||||||
export class ErrorNotificationService {
|
export class ErrorNotificationService {
|
||||||
constructor(private toastr: ToastrService) {}
|
constructor(private toastr: ToastrService) {}
|
||||||
|
private isToastVisible = false;
|
||||||
|
|
||||||
handleHttpError(error: unknown): void {
|
handleHttpError(error: unknown): void {
|
||||||
|
if (this.isToastVisible) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let errorMessage = 'An unknown error occurred.';
|
let errorMessage = 'An unknown error occurred.';
|
||||||
|
|
||||||
if (error instanceof HttpErrorResponse) {
|
if (error instanceof HttpErrorResponse) {
|
||||||
|
|
@ -22,8 +27,19 @@ export class ErrorNotificationService {
|
||||||
'The request could not be processed due to invalid input. Please check your entries and try again.';
|
'The request could not be processed due to invalid input. Please check your entries and try again.';
|
||||||
break;
|
break;
|
||||||
case 401:
|
case 401:
|
||||||
|
console.log(error);
|
||||||
|
|
||||||
|
if (
|
||||||
|
error.error.detail &&
|
||||||
|
(error.error.detail.includes('Token') ||
|
||||||
|
error.error.detail.includes('token'))
|
||||||
|
) {
|
||||||
|
errorMessage =
|
||||||
|
'The authentication token is invalid or expired. Please log in again.';
|
||||||
|
} else {
|
||||||
errorMessage =
|
errorMessage =
|
||||||
'The login credentials are incorrect. Please check your email and password.';
|
'The login credentials are incorrect. Please check your email and password.';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 403:
|
case 403:
|
||||||
errorMessage =
|
errorMessage =
|
||||||
|
|
@ -59,10 +75,16 @@ export class ErrorNotificationService {
|
||||||
errorMessage = `Network error: ${error.message || error.toString()}`;
|
errorMessage = `Network error: ${error.message || error.toString()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.isToastVisible = true;
|
||||||
|
|
||||||
this.toastr.error(errorMessage, 'Error', {
|
this.toastr.error(errorMessage, 'Error', {
|
||||||
timeOut: 3000,
|
timeOut: 3000,
|
||||||
positionClass: 'toast-top-right',
|
positionClass: 'toast-top-right',
|
||||||
progressBar: true,
|
progressBar: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
this.isToastVisible = false;
|
||||||
|
}, 3000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue