change authenticate service, log in

This commit is contained in:
mchouliara 2024-09-10 17:21:29 +03:00
parent 2d7c3d0f24
commit d139b905d9
4 changed files with 25 additions and 16 deletions

View File

@ -10,9 +10,9 @@ import { BaseHttpParams } from '@common/http/base-http-params';
import { InterceptorType } from '@common/http/interceptors/interceptor-type'; import { InterceptorType } from '@common/http/interceptors/interceptor-type';
import { Guid } from '@common/types/guid'; import { Guid } from '@common/types/guid';
import { TranslateService } from '@ngx-translate/core'; 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 { 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 { ConfigurationService } from '../configuration/configuration.service';
import { PrincipalService } from '../http/principal.service'; import { PrincipalService } from '../http/principal.service';
import { TenantHandlingService } from '../tenant/tenant-handling.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<KeycloakEvent | null> {
if (!this.keycloakService.isLoggedIn()) { if (!this.keycloakService.isLoggedIn()) {
this.keycloakService.login({ this.keycloakService.login({
scope: this.installationConfiguration.keycloak.scope, scope: this.installationConfiguration.keycloak.scope,
}) })
.then(() => { .then(() => {
this.keycloakService.keycloakEvents$.subscribe({ return this.keycloakService.keycloakEvents$.pipe(
next: (e) => { tap((e) => {
if ( if (
e.type === KeycloakEventType.OnTokenExpired e.type === KeycloakEventType.OnTokenExpired
) { ) {
@ -313,13 +313,13 @@ export class AuthService extends BaseService {
throw x; throw x;
}); });
} }
}, }),
}); );
this.onAuthenticateSuccess(returnUrl);
}) })
.catch((error) => this.onAuthenticateError(error)); .catch((error) => this.onAuthenticateError(error));
} else { } else {
this.zone.run(() => this.router.navigateByUrl(returnUrl)); this.zone.run(() => this.router.navigateByUrl(returnUrl));
return of();
} }
} }
@ -361,10 +361,6 @@ export class AuthService extends BaseService {
onAuthenticateSuccess(returnUrl: string): void { onAuthenticateSuccess(returnUrl: string): void {
this.authState(true); this.authState(true);
// this.uiNotificationService.snackBarNotification(
// this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-LOGIN'),
// SnackBarNotificationLevel.Success
// );
this.zone.run(() => this.router.navigateByUrl(returnUrl)); this.zone.run(() => this.router.navigateByUrl(returnUrl));
} }

View File

@ -1,9 +1,11 @@
import { Component, Input, NgZone, OnInit } from '@angular/core'; import { Component, Input, NgZone, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router'; import { ActivatedRoute, Router } from '@angular/router';
import { AuthService } from '@app/core/services/auth/auth.service'; 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 { RouterUtilsService } from '@app/core/services/router/router-utils.service';
import { TenantHandlingService } from '@app/core/services/tenant/tenant-handling.service'; import { TenantHandlingService } from '@app/core/services/tenant/tenant-handling.service';
import { BaseComponent } from '@common/base/base.component'; import { BaseComponent } from '@common/base/base.component';
import { TranslateService } from '@ngx-translate/core';
import { KeycloakService } from 'keycloak-angular'; import { KeycloakService } from 'keycloak-angular';
import { from } from 'rxjs'; import { from } from 'rxjs';
import { takeUntil } from 'rxjs/operators'; import { takeUntil } from 'rxjs/operators';
@ -28,13 +30,25 @@ export class LoginComponent extends BaseComponent implements OnInit {
private authService: AuthService, private authService: AuthService,
private route: ActivatedRoute, private route: ActivatedRoute,
private tenantHandlingService: TenantHandlingService, private tenantHandlingService: TenantHandlingService,
private keycloakService: KeycloakService private keycloakService: KeycloakService,
private uiNotificationService: UiNotificationService,
private language: TranslateService
) { super(); } ) { super(); }
ngOnInit(): void { ngOnInit(): void {
this.returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') || '/'; this.returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') || '/';
if (!this.keycloakService.isLoggedIn()) { 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 { } else {
const tenantCode = this.tenantHandlingService.extractTenantCodeFromUrlPath(this.returnUrl) ?? this.authService.selectedTenant() ?? 'default'; const tenantCode = this.tenantHandlingService.extractTenantCodeFromUrlPath(this.returnUrl) ?? this.authService.selectedTenant() ?? 'default';
let returnUrL = this.returnUrl; let returnUrL = this.returnUrl;

View File

@ -13,7 +13,7 @@ import { MergeEmailLoaderDialogComponent } from './merge-email-confirmation/merg
imports: [ imports: [
CommonUiModule, CommonUiModule,
CommonFormsModule, CommonFormsModule,
LoginRoutingModule LoginRoutingModule,
], ],
declarations: [ declarations: [
LoginComponent, LoginComponent,

View File

@ -19,7 +19,6 @@ export class LanguageComponent extends BaseComponent implements OnInit {
constructor( constructor(
private router: Router, private router: Router,
private authentication: AuthService, private authentication: AuthService,
private userService: UserService,
private languageService: LanguageService private languageService: LanguageService
) { ) {
super(); super();