From d139b905d99db9bf97624d21011356a494590d42 Mon Sep 17 00:00:00 2001 From: mchouliara Date: Tue, 10 Sep 2024 17:21:29 +0300 Subject: [PATCH] change authenticate service, log in --- .../app/core/services/auth/auth.service.ts | 20 ++++++++----------- .../src/app/ui/auth/login/login.component.ts | 18 +++++++++++++++-- .../src/app/ui/auth/login/login.module.ts | 2 +- .../language-content/language.component.ts | 1 - 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/frontend/src/app/core/services/auth/auth.service.ts b/frontend/src/app/core/services/auth/auth.service.ts index d2a96f398..8cf88bcc1 100644 --- a/frontend/src/app/core/services/auth/auth.service.ts +++ b/frontend/src/app/core/services/auth/auth.service.ts @@ -10,9 +10,9 @@ import { BaseHttpParams } from '@common/http/base-http-params'; import { InterceptorType } from '@common/http/interceptors/interceptor-type'; import { Guid } from '@common/types/guid'; import { TranslateService } from '@ngx-translate/core'; -import { KeycloakEventType, KeycloakService } from 'keycloak-angular'; +import { KeycloakEvent, KeycloakEventType, KeycloakService } from 'keycloak-angular'; import { Observable, Subject, forkJoin, from, of } from 'rxjs'; -import { concatMap, exhaustMap, map, takeUntil } from 'rxjs/operators'; +import { concatMap, exhaustMap, map, takeUntil, tap } from 'rxjs/operators'; import { ConfigurationService } from '../configuration/configuration.service'; import { PrincipalService } from '../http/principal.service'; import { TenantHandlingService } from '../tenant/tenant-handling.service'; @@ -291,14 +291,14 @@ export class AuthService extends BaseService { } - public authenticate(returnUrl: string) { + public authenticate(returnUrl: string): Observable { if (!this.keycloakService.isLoggedIn()) { this.keycloakService.login({ scope: this.installationConfiguration.keycloak.scope, }) .then(() => { - this.keycloakService.keycloakEvents$.subscribe({ - next: (e) => { + return this.keycloakService.keycloakEvents$.pipe( + tap((e) => { if ( e.type === KeycloakEventType.OnTokenExpired ) { @@ -313,13 +313,13 @@ export class AuthService extends BaseService { throw x; }); } - }, - }); - this.onAuthenticateSuccess(returnUrl); + }), + ); }) .catch((error) => this.onAuthenticateError(error)); } else { this.zone.run(() => this.router.navigateByUrl(returnUrl)); + return of(); } } @@ -361,10 +361,6 @@ export class AuthService extends BaseService { onAuthenticateSuccess(returnUrl: string): void { this.authState(true); - // this.uiNotificationService.snackBarNotification( - // this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-LOGIN'), - // SnackBarNotificationLevel.Success - // ); this.zone.run(() => this.router.navigateByUrl(returnUrl)); } diff --git a/frontend/src/app/ui/auth/login/login.component.ts b/frontend/src/app/ui/auth/login/login.component.ts index a0bf187fa..24ad9f014 100644 --- a/frontend/src/app/ui/auth/login/login.component.ts +++ b/frontend/src/app/ui/auth/login/login.component.ts @@ -1,9 +1,11 @@ import { Component, Input, NgZone, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { AuthService } from '@app/core/services/auth/auth.service'; +import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { RouterUtilsService } from '@app/core/services/router/router-utils.service'; import { TenantHandlingService } from '@app/core/services/tenant/tenant-handling.service'; import { BaseComponent } from '@common/base/base.component'; +import { TranslateService } from '@ngx-translate/core'; import { KeycloakService } from 'keycloak-angular'; import { from } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; @@ -28,13 +30,25 @@ export class LoginComponent extends BaseComponent implements OnInit { private authService: AuthService, private route: ActivatedRoute, private tenantHandlingService: TenantHandlingService, - private keycloakService: KeycloakService + private keycloakService: KeycloakService, + private uiNotificationService: UiNotificationService, + private language: TranslateService ) { super(); } ngOnInit(): void { this.returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') || '/'; if (!this.keycloakService.isLoggedIn()) { - this.authService.authenticate(this.returnUrl); + this.authService.authenticate(this.returnUrl) + .pipe(takeUntil(this._destroyed)) + .subscribe({ + next: () => { + this.uiNotificationService.snackBarNotification( //maybe remove this + this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-LOGIN'), + SnackBarNotificationLevel.Success + ); + this.authService.onAuthenticateSuccess(this.returnUrl); + } + }); } else { const tenantCode = this.tenantHandlingService.extractTenantCodeFromUrlPath(this.returnUrl) ?? this.authService.selectedTenant() ?? 'default'; let returnUrL = this.returnUrl; diff --git a/frontend/src/app/ui/auth/login/login.module.ts b/frontend/src/app/ui/auth/login/login.module.ts index 3c261d883..89bd1d5f1 100644 --- a/frontend/src/app/ui/auth/login/login.module.ts +++ b/frontend/src/app/ui/auth/login/login.module.ts @@ -13,7 +13,7 @@ import { MergeEmailLoaderDialogComponent } from './merge-email-confirmation/merg imports: [ CommonUiModule, CommonFormsModule, - LoginRoutingModule + LoginRoutingModule, ], declarations: [ LoginComponent, diff --git a/frontend/src/app/ui/language/language-content/language.component.ts b/frontend/src/app/ui/language/language-content/language.component.ts index d21dc2701..e0b5c7210 100644 --- a/frontend/src/app/ui/language/language-content/language.component.ts +++ b/frontend/src/app/ui/language/language-content/language.component.ts @@ -19,7 +19,6 @@ export class LanguageComponent extends BaseComponent implements OnInit { constructor( private router: Router, private authentication: AuthService, - private userService: UserService, private languageService: LanguageService ) { super();