feat: added ngx-toastr notifications for login and logout success messages

This commit is contained in:
Chneemann 2025-03-26 13:09:25 +01:00
parent dd90cf6e58
commit 5422835c06
4 changed files with 30 additions and 3 deletions

View file

@ -24,6 +24,7 @@ export const routes: Routes = [
children: [ children: [
{ path: '', component: LoginComponent, pathMatch: 'full' }, { path: '', component: LoginComponent, pathMatch: 'full' },
{ path: 'login', component: LoginComponent }, { path: 'login', component: LoginComponent },
{ path: 'logout', component: LoginComponent },
{ path: 'login/notice/:id', component: LoginComponent }, { path: 'login/notice/:id', component: LoginComponent },
{ path: 'register', component: RegisterComponent }, { path: 'register', component: RegisterComponent },
{ path: 'forgot-pw', component: ForgotPwComponent }, { path: 'forgot-pw', component: ForgotPwComponent },

View file

@ -78,6 +78,7 @@ export class LoginComponent {
try { try {
await this.authService.login(this.loginData, this.checkboxRememberMe); await this.authService.login(this.loginData, this.checkboxRememberMe);
this.router.navigate(['/summary']); this.router.navigate(['/summary']);
this.sharedService.isBtnDisabled = false;
} catch (error) { } catch (error) {
this.sharedService.isBtnDisabled = false; this.sharedService.isBtnDisabled = false;
const errorMessage = this.errorHandlingService.handleHttpError(error); const errorMessage = this.errorHandlingService.handleHttpError(error);

View file

@ -24,7 +24,6 @@ export class RedirectIfAuthenticatedGuard {
return this.authService.checkAuthUser().pipe( return this.authService.checkAuthUser().pipe(
map((isAuthenticated) => { map((isAuthenticated) => {
if (isAuthenticated) { if (isAuthenticated) {
this.router.navigate(['/summary']);
return false; return false;
} }
return true; return true;

View file

@ -10,6 +10,8 @@ import {
import { ApiService } from './api.service'; import { ApiService } from './api.service';
import { TokenService } from './token.service'; import { TokenService } from './token.service';
import { ErrorHandlingService } from './error-handling.service'; import { ErrorHandlingService } from './error-handling.service';
import { ToastrService } from 'ngx-toastr';
import { Router } from '@angular/router';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -20,7 +22,9 @@ export class AuthService {
constructor( constructor(
private apiService: ApiService, private apiService: ApiService,
private tokenService: TokenService, private tokenService: TokenService,
private errorHandlingService: ErrorHandlingService private errorHandlingService: ErrorHandlingService,
private toastrService: ToastrService,
private router: Router
) { ) {
this.currentUserIdSubject.next(this.tokenService.getUserId()); this.currentUserIdSubject.next(this.tokenService.getUserId());
} }
@ -38,6 +42,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();
} catch (error) { } catch (error) {
console.error('Login failed:', error); console.error('Login failed:', error);
throw new Error(this.errorHandlingService.handleHttpError(error)); throw new Error(this.errorHandlingService.handleHttpError(error));
@ -51,7 +56,8 @@ export class AuthService {
this.tokenService.deleteAuthToken(); this.tokenService.deleteAuthToken();
this.tokenService.deleteUserId(); this.tokenService.deleteUserId();
this.currentUserIdSubject.next(null); this.currentUserIdSubject.next(null);
window.location.href = '/login'; this.showLogoutSuccessMessage();
this.router.navigate(['/logout']);
} catch (error) { } catch (error) {
console.error('Logout failed:', error); console.error('Logout failed:', error);
throw new Error(this.errorHandlingService.handleHttpError(error)); throw new Error(this.errorHandlingService.handleHttpError(error));
@ -68,4 +74,24 @@ 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,
}
);
}
} }