refactor: move success toast notifications for login and logout to ErrorHandlingService.
This commit is contained in:
parent
76594ca28f
commit
6e0429255b
2 changed files with 22 additions and 24 deletions
|
|
@ -23,7 +23,6 @@ export class AuthService {
|
||||||
private apiService: ApiService,
|
private apiService: ApiService,
|
||||||
private tokenService: TokenService,
|
private tokenService: TokenService,
|
||||||
private errorHandlingService: ErrorHandlingService,
|
private errorHandlingService: ErrorHandlingService,
|
||||||
private toastrService: ToastrService,
|
|
||||||
private router: Router
|
private router: Router
|
||||||
) {
|
) {
|
||||||
this.currentUserIdSubject.next(this.tokenService.getUserId());
|
this.currentUserIdSubject.next(this.tokenService.getUserId());
|
||||||
|
|
@ -42,7 +41,7 @@ export class AuthService {
|
||||||
this.tokenService.storeAuthToken(token, storage);
|
this.tokenService.storeAuthToken(token, storage);
|
||||||
this.tokenService.storeUserId(userId, storage);
|
this.tokenService.storeUserId(userId, storage);
|
||||||
this.currentUserIdSubject.next(userId);
|
this.currentUserIdSubject.next(userId);
|
||||||
this.showLoginSuccessMessage();
|
this.errorHandlingService.loginSuccessToast();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed:', error);
|
console.error('Login failed:', error);
|
||||||
this.errorHandlingService.handleHttpError(error);
|
this.errorHandlingService.handleHttpError(error);
|
||||||
|
|
@ -56,7 +55,7 @@ export class AuthService {
|
||||||
this.tokenService.deleteAuthToken();
|
this.tokenService.deleteAuthToken();
|
||||||
this.tokenService.deleteUserId();
|
this.tokenService.deleteUserId();
|
||||||
this.currentUserIdSubject.next(null);
|
this.currentUserIdSubject.next(null);
|
||||||
this.showLogoutSuccessMessage();
|
this.errorHandlingService.logoutSuccessToast();
|
||||||
this.router.navigate(['/logout']);
|
this.router.navigate(['/logout']);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Logout failed:', error);
|
console.error('Logout failed:', error);
|
||||||
|
|
@ -74,24 +73,4 @@ export class AuthService {
|
||||||
getCurrentUserId(): Observable<string | null> {
|
getCurrentUserId(): Observable<string | null> {
|
||||||
return this.currentUserIdSubject.asObservable();
|
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,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,26 @@ export class ErrorHandlingService {
|
||||||
errorMessage = `${error.message || error.toString()}`;
|
errorMessage = `${error.message || error.toString()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show the toast message with the error message
|
|
||||||
this.toastr.error(errorMessage, 'Error');
|
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,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue