refactor: move success toast notifications for login and logout to ErrorHandlingService.

This commit is contained in:
Chneemann 2025-03-26 13:19:22 +01:00
parent 76594ca28f
commit 6e0429255b
2 changed files with 22 additions and 24 deletions

View file

@ -23,7 +23,6 @@ export class AuthService {
private apiService: ApiService,
private tokenService: TokenService,
private errorHandlingService: ErrorHandlingService,
private toastrService: ToastrService,
private router: Router
) {
this.currentUserIdSubject.next(this.tokenService.getUserId());
@ -42,7 +41,7 @@ export class AuthService {
this.tokenService.storeAuthToken(token, storage);
this.tokenService.storeUserId(userId, storage);
this.currentUserIdSubject.next(userId);
this.showLoginSuccessMessage();
this.errorHandlingService.loginSuccessToast();
} catch (error) {
console.error('Login failed:', error);
this.errorHandlingService.handleHttpError(error);
@ -56,7 +55,7 @@ export class AuthService {
this.tokenService.deleteAuthToken();
this.tokenService.deleteUserId();
this.currentUserIdSubject.next(null);
this.showLogoutSuccessMessage();
this.errorHandlingService.logoutSuccessToast();
this.router.navigate(['/logout']);
} catch (error) {
console.error('Logout failed:', error);
@ -74,24 +73,4 @@ export class AuthService {
getCurrentUserId(): Observable<string | null> {
return this.currentUserIdSubject.asObservable();
}
private showLoginSuccessMessage() {
this.toastrService.success(
'You have successfully logged in.',
'Login Successful',
{
timeOut: 3000,
}
);
}
private showLogoutSuccessMessage() {
this.toastrService.info(
'You have successfully logged out. Have a nice day!',
'Logout Successful',
{
timeOut: 3000,
}
);
}
}

View file

@ -38,7 +38,26 @@ export class ErrorHandlingService {
errorMessage = `${error.message || error.toString()}`;
}
// Show the toast message with the error message
this.toastr.error(errorMessage, 'Error');
}
loginSuccessToast(): void {
this.toastr.success(
'You have successfully logged in.',
'Login Successful',
{
timeOut: 3000,
}
);
}
logoutSuccessToast(): void {
this.toastr.info(
'You have successfully logged out. Have a nice day!',
'Logout Successful',
{
timeOut: 3000,
}
);
}
}