From bed3635eab3ad8941e08b040551dd6fc81e57e99 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 9 Mar 2021 18:02:54 +0300 Subject: [PATCH] make some improvements for auth strategies --- .../dev-app/src/app/home/home.component.ts | 2 +- .../account/src/lib/account.module.ts | 2 - .../lib/components/login/login.component.html | 4 +- .../lib/components/login/login.component.ts | 26 +++--- .../components/register/register.component.ts | 24 ++++-- .../lib/guards/authentication-flow.guard.ts | 2 +- .../lib/services/authentication.service.ts | 55 ------------ .../account/src/lib/services/index.ts | 1 - .../account/src/lib/utils/auth-utils.ts | 9 ++ .../packages/account/src/lib/utils/index.ts | 1 + .../packages/core/src/lib/core.module.ts | 2 - .../core/src/lib/guards/auth.guard.ts | 2 +- .../src/lib/services/auth-flow-initializer.ts | 30 ------- .../core/src/lib/services/auth.service.ts | 50 +++++++---- .../packages/core/src/lib/services/index.ts | 1 - .../src/lib/strategies/auth-flow.strategy.ts | 85 ++++++++++++------- .../packages/core/src/lib/strategies/index.ts | 1 - .../core/src/lib/tests/auth.guard.spec.ts | 6 +- .../core/src/lib/utils/initial-utils.ts | 6 +- .../nav-items/current-user.component.ts | 6 +- .../src/lib/handlers/error.handler.ts | 2 +- .../angular/src/app/home/home.component.ts | 2 +- 22 files changed, 145 insertions(+), 174 deletions(-) delete mode 100644 npm/ng-packs/packages/account/src/lib/services/authentication.service.ts create mode 100644 npm/ng-packs/packages/account/src/lib/utils/auth-utils.ts delete mode 100644 npm/ng-packs/packages/core/src/lib/services/auth-flow-initializer.ts diff --git a/npm/ng-packs/apps/dev-app/src/app/home/home.component.ts b/npm/ng-packs/apps/dev-app/src/app/home/home.component.ts index 3790f8fe3f..c0b4602ee2 100644 --- a/npm/ng-packs/apps/dev-app/src/app/home/home.component.ts +++ b/npm/ng-packs/apps/dev-app/src/app/home/home.component.ts @@ -14,6 +14,6 @@ export class HomeComponent { constructor(private oAuthService: OAuthService, private authService: AuthService) {} login() { - this.authService.initLogin(); + this.authService.navigateToLogin(); } } diff --git a/npm/ng-packs/packages/account/src/lib/account.module.ts b/npm/ng-packs/packages/account/src/lib/account.module.ts index 20324d07cf..2d36961b97 100644 --- a/npm/ng-packs/packages/account/src/lib/account.module.ts +++ b/npm/ng-packs/packages/account/src/lib/account.module.ts @@ -15,7 +15,6 @@ import { AccountConfigOptions } from './models/config-options'; import { ACCOUNT_CONFIG_OPTIONS } from './tokens/config-options.token'; import { accountConfigOptionsFactory } from './utils/factory-utils'; import { AuthenticationFlowGuard } from './guards/authentication-flow.guard'; -import { AuthenticationService } from './services/authentication.service'; import { ForgotPasswordComponent } from './components/forgot-password/forgot-password.component'; import { ResetPasswordComponent } from './components/reset-password/reset-password.component'; @@ -48,7 +47,6 @@ export class AccountModule { ngModule: AccountModule, providers: [ AuthenticationFlowGuard, - AuthenticationService, { provide: ACCOUNT_CONFIG_OPTIONS, useValue: options }, { provide: 'ACCOUNT_OPTIONS', diff --git a/npm/ng-packs/packages/account/src/lib/components/login/login.component.html b/npm/ng-packs/packages/account/src/lib/components/login/login.component.html index 061d182ccf..d2d9712421 100644 --- a/npm/ng-packs/packages/account/src/lib/components/login/login.component.html +++ b/npm/ng-packs/packages/account/src/lib/components/login/login.component.html @@ -1,7 +1,7 @@

{{ 'AbpAccount::Login' | abpLocalization }}

{{ 'AbpAccount::AreYouANewUser' | abpLocalization }} - {{ + {{ 'AbpAccount::Register' | abpLocalization }} @@ -37,7 +37,7 @@ class="form-check-input" type="checkbox" id="login-input-remember-me" - formControlName="remember" + formControlName="rememberMe" /> {{ 'AbpAccount::RememberMe' | abpLocalization }} diff --git a/npm/ng-packs/packages/account/src/lib/components/login/login.component.ts b/npm/ng-packs/packages/account/src/lib/components/login/login.component.ts index 3412e2046d..7ef04e31a9 100644 --- a/npm/ng-packs/packages/account/src/lib/components/login/login.component.ts +++ b/npm/ng-packs/packages/account/src/lib/components/login/login.component.ts @@ -1,13 +1,13 @@ -import { ConfigStateService } from '@abp/ng.core'; +import { ConfigStateService, AuthService } from '@abp/ng.core'; import { ToasterService } from '@abp/ng.theme.shared'; -import { Component, OnInit } from '@angular/core'; +import { Component, Injector, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Store } from '@ngxs/store'; import { throwError } from 'rxjs'; import { catchError, finalize } from 'rxjs/operators'; import snq from 'snq'; import { eAccountComponents } from '../../enums/components'; -import { AuthenticationService } from '../../services/authentication.service'; +import { getRedirectUrl } from '../../utils/auth-utils'; const { maxLength, required } = Validators; @@ -25,11 +25,11 @@ export class LoginComponent implements OnInit { authWrapperKey = eAccountComponents.AuthWrapper; constructor( - private fb: FormBuilder, - private store: Store, - private toasterService: ToasterService, - private authenticationService: AuthenticationService, - private configState: ConfigStateService, + protected injector: Injector, + protected fb: FormBuilder, + protected toasterService: ToasterService, + protected authService: AuthService, + protected configState: ConfigStateService, ) {} ngOnInit() { @@ -48,7 +48,7 @@ export class LoginComponent implements OnInit { this.form = this.fb.group({ username: ['', [required, maxLength(255)]], password: ['', [required, maxLength(128)]], - remember: [false], + rememberMe: [false], }); } @@ -57,10 +57,12 @@ export class LoginComponent implements OnInit { this.inProgress = true; - const { username, password, remember } = this.form.value; + const { username, password, rememberMe } = this.form.value; - this.authenticationService - .login(username, password, remember) + const redirectUrl = getRedirectUrl(this.injector); + + this.authService + .login({ username, password, rememberMe, redirectUrl }) .pipe( catchError(err => { this.toasterService.error( diff --git a/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts b/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts index 63454c3233..9be22d2203 100644 --- a/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts +++ b/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts @@ -1,4 +1,4 @@ -import { ConfigStateService } from '@abp/ng.core'; +import { AuthService, ConfigStateService } from '@abp/ng.core'; import { getPasswordValidators, ToasterService } from '@abp/ng.theme.shared'; import { Component, Injector, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @@ -8,7 +8,7 @@ import snq from 'snq'; import { eAccountComponents } from '../../enums/components'; import { AccountService } from '../../proxy/account/account.service'; import { RegisterDto } from '../../proxy/account/models'; -import { AuthenticationService } from '../../services'; +import { getRedirectUrl } from '../../utils/auth-utils'; const { maxLength, required, email } = Validators; @Component({ @@ -25,12 +25,12 @@ export class RegisterComponent implements OnInit { authWrapperKey = eAccountComponents.AuthWrapper; constructor( - private fb: FormBuilder, - private accountService: AccountService, - private configState: ConfigStateService, - private toasterService: ToasterService, - private authenticationSerivice: AuthenticationService, - private injector: Injector, + protected fb: FormBuilder, + protected accountService: AccountService, + protected configState: ConfigStateService, + protected toasterService: ToasterService, + protected authService: AuthService, + protected injector: Injector, ) {} ngOnInit() { @@ -79,7 +79,13 @@ export class RegisterComponent implements OnInit { this.accountService .register(newUser) .pipe( - switchMap(() => this.authenticationSerivice.login(newUser.userName, newUser.password)), + switchMap(() => + this.authService.login({ + username: newUser.userName, + password: newUser.password, + redirectUrl: getRedirectUrl(this.injector), + }), + ), catchError(err => { this.toasterService.error( snq(() => err.error.error_description) || diff --git a/npm/ng-packs/packages/account/src/lib/guards/authentication-flow.guard.ts b/npm/ng-packs/packages/account/src/lib/guards/authentication-flow.guard.ts index 7cb719628e..6828615403 100644 --- a/npm/ng-packs/packages/account/src/lib/guards/authentication-flow.guard.ts +++ b/npm/ng-packs/packages/account/src/lib/guards/authentication-flow.guard.ts @@ -9,7 +9,7 @@ export class AuthenticationFlowGuard implements CanActivate { canActivate() { if (this.authService.isInternalAuth) return true; - this.authService.initLogin(); + this.authService.navigateToLogin(); return false; } } diff --git a/npm/ng-packs/packages/account/src/lib/services/authentication.service.ts b/npm/ng-packs/packages/account/src/lib/services/authentication.service.ts deleted file mode 100644 index ab8223e591..0000000000 --- a/npm/ng-packs/packages/account/src/lib/services/authentication.service.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { - AbpApplicationConfigurationService, - AuthPasswordFlowStrategy, - AuthService, - ConfigStateService, - RestService, - SessionStateService, -} from '@abp/ng.core'; -import { HttpHeaders } from '@angular/common/http'; -import { Inject, Injectable } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; -import { OAuthService } from 'angular-oauth2-oidc'; -import { from, Observable } from 'rxjs'; -import { switchMap, take, tap } from 'rxjs/operators'; -import { AccountConfigOptions } from '../models/config-options'; -import { ACCOUNT_CONFIG_OPTIONS } from '../tokens/config-options.token'; - -@Injectable() -export class AuthenticationService { - constructor( - protected rest: RestService, - protected sessionState: SessionStateService, - protected authService: AuthService, - protected oAuthService: OAuthService, - protected appConfigService: AbpApplicationConfigurationService, - protected configState: ConfigStateService, - @Inject(ACCOUNT_CONFIG_OPTIONS) protected options: AccountConfigOptions, - protected router: Router, - protected route: ActivatedRoute, - ) {} - - login(username: string, password: string, remember = false): Observable { - const tenant = this.sessionState.getTenant(); - - return from( - this.oAuthService.fetchTokenUsingPasswordFlow( - username, - password, - new HttpHeaders({ ...(tenant && tenant.id && { __tenant: tenant.id }) }), - ), - ).pipe( - switchMap(() => this.appConfigService.get()), - tap(res => { - this.configState.setState(res); - - const redirectUrl = - this.route.snapshot.queryParams.returnUrl || this.options.redirectUrl || '/'; - - this.router.navigate([redirectUrl]); - - this.authService.strategy.setRememberMe(remember); - }), - ); - } -} diff --git a/npm/ng-packs/packages/account/src/lib/services/index.ts b/npm/ng-packs/packages/account/src/lib/services/index.ts index 12e6331345..b94bc7c840 100644 --- a/npm/ng-packs/packages/account/src/lib/services/index.ts +++ b/npm/ng-packs/packages/account/src/lib/services/index.ts @@ -1,2 +1 @@ -export * from './authentication.service'; export * from './manage-profile.state.service'; diff --git a/npm/ng-packs/packages/account/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/account/src/lib/utils/auth-utils.ts new file mode 100644 index 0000000000..6dba19c83f --- /dev/null +++ b/npm/ng-packs/packages/account/src/lib/utils/auth-utils.ts @@ -0,0 +1,9 @@ +import { Injector } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { ACCOUNT_CONFIG_OPTIONS } from '../tokens'; + +export function getRedirectUrl(injector: Injector) { + const route = injector.get(ActivatedRoute); + const options = injector.get(ACCOUNT_CONFIG_OPTIONS); + return route.snapshot.queryParams.returnUrl || options.redirectUrl || '/'; +} diff --git a/npm/ng-packs/packages/account/src/lib/utils/index.ts b/npm/ng-packs/packages/account/src/lib/utils/index.ts index ed99d0aaa4..9235af99fd 100644 --- a/npm/ng-packs/packages/account/src/lib/utils/index.ts +++ b/npm/ng-packs/packages/account/src/lib/utils/index.ts @@ -1 +1,2 @@ +export * from './auth-utils'; export * from './factory-utils'; diff --git a/npm/ng-packs/packages/core/src/lib/core.module.ts b/npm/ng-packs/packages/core/src/lib/core.module.ts index 5c2e60d7c7..986cb98f3b 100644 --- a/npm/ng-packs/packages/core/src/lib/core.module.ts +++ b/npm/ng-packs/packages/core/src/lib/core.module.ts @@ -26,7 +26,6 @@ import { ABP } from './models/common'; import { LocalizationPipe } from './pipes/localization.pipe'; import { SortPipe } from './pipes/sort.pipe'; import { LocaleProvider } from './providers/locale.provider'; -import { AuthFlowInitializer } from './services/auth-flow-initializer'; import { LocalizationService } from './services/localization.service'; import { ProfileState } from './states/profile.state'; import { oAuthStorage } from './strategies/auth-flow.strategy'; @@ -179,7 +178,6 @@ export class CoreModule { useFactory: noop, }, { provide: OAuthStorage, useFactory: storageFactory }, - AuthFlowInitializer, ], }; } diff --git a/npm/ng-packs/packages/core/src/lib/guards/auth.guard.ts b/npm/ng-packs/packages/core/src/lib/guards/auth.guard.ts index c95b890239..f1e73ab805 100644 --- a/npm/ng-packs/packages/core/src/lib/guards/auth.guard.ts +++ b/npm/ng-packs/packages/core/src/lib/guards/auth.guard.ts @@ -16,7 +16,7 @@ export class AuthGuard implements CanActivate { return true; } - this.authService.initLogin(); + this.authService.navigateToLogin(); return false; } } diff --git a/npm/ng-packs/packages/core/src/lib/services/auth-flow-initializer.ts b/npm/ng-packs/packages/core/src/lib/services/auth-flow-initializer.ts deleted file mode 100644 index 5dbdde97a5..0000000000 --- a/npm/ng-packs/packages/core/src/lib/services/auth-flow-initializer.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Injectable, Injector } from '@angular/core'; -import { from, Observable } from 'rxjs'; -import { filter, map, switchMap, take, tap } from 'rxjs/operators'; -import { AUTH_FLOW_STRATEGY } from '../strategies/auth-flow.strategy'; -import { AuthService } from './auth.service'; -import { EnvironmentService } from './environment.service'; - -@Injectable() -export class AuthFlowInitializer { - constructor( - protected injector: Injector, - protected environmentService: EnvironmentService, - protected authService: AuthService, - ) {} - - init(): Observable | Promise { - return this.environmentService.getEnvironment$().pipe( - map(env => env?.oAuthConfig), - filter(oAuthConfig => !!oAuthConfig), - tap(oAuthConfig => { - this.authService.strategy = - oAuthConfig.responseType === 'code' - ? AUTH_FLOW_STRATEGY.Code(this.injector) - : AUTH_FLOW_STRATEGY.Password(this.injector); - }), - switchMap(() => from(this.authService.init())), - take(1), - ); - } -} diff --git a/npm/ng-packs/packages/core/src/lib/services/auth.service.ts b/npm/ng-packs/packages/core/src/lib/services/auth.service.ts index 3a24bd1538..b12794bc80 100644 --- a/npm/ng-packs/packages/core/src/lib/services/auth.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/auth.service.ts @@ -1,10 +1,10 @@ import { Injectable, Injector } from '@angular/core'; -import { Observable } from 'rxjs'; +import { from, Observable } from 'rxjs'; +import { filter, map, switchMap, take, tap } from 'rxjs/operators'; import { - AuthCodeFlowStrategy, AuthFlowStrategy, - AuthPasswordFlowStrategy, AUTH_FLOW_STRATEGY, + LoginParams, } from '../strategies/auth-flow.strategy'; import { EnvironmentService } from './environment.service'; @@ -12,30 +12,50 @@ import { EnvironmentService } from './environment.service'; providedIn: 'root', }) export class AuthService { - private _strategy: AuthFlowStrategy; - - set strategy(strategy: AuthFlowStrategy) { - if (this.strategy) this.strategy.destroy(); - this._strategy = strategy; - } - - get strategy() { - return this._strategy; - } + private strategy: AuthFlowStrategy; get isInternalAuth() { return this.strategy.isInternalAuth; } + constructor(protected injector: Injector) {} + async init() { - return await this.strategy.init(); + const environmentService = this.injector.get(EnvironmentService); + + return environmentService + .getEnvironment$() + .pipe( + map(env => env?.oAuthConfig), + filter(oAuthConfig => !!oAuthConfig), + tap(oAuthConfig => { + this.strategy = + oAuthConfig.responseType === 'code' + ? AUTH_FLOW_STRATEGY.Code(this.injector) + : AUTH_FLOW_STRATEGY.Password(this.injector); + }), + switchMap(() => from(this.strategy.init())), + take(1), + ) + .toPromise(); } logout(): Observable { return this.strategy.logout(); } + /** + * @deprecated Use navigateToLogin method instead. To be deleted in v5.0 + */ initLogin() { - this.strategy.login(); + this.strategy.navigateToLogin(); + } + + navigateToLogin() { + this.strategy.navigateToLogin(); + } + + login(params: LoginParams) { + return this.strategy.login(params); } } diff --git a/npm/ng-packs/packages/core/src/lib/services/index.ts b/npm/ng-packs/packages/core/src/lib/services/index.ts index 5301cd6fe8..55000edde1 100644 --- a/npm/ng-packs/packages/core/src/lib/services/index.ts +++ b/npm/ng-packs/packages/core/src/lib/services/index.ts @@ -1,6 +1,5 @@ export * from './application-configuration.service'; export * from './auth.service'; -export * from './auth-flow-initializer'; export * from './config-state.service'; export * from './content-projection.service'; export * from './dom-insertion.service'; diff --git a/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts b/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts index 0a676fe2b7..3b59c4d6ff 100644 --- a/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts +++ b/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts @@ -1,3 +1,4 @@ +import { HttpHeaders } from '@angular/common/http'; import { Injector } from '@angular/core'; import { Router } from '@angular/router'; import { Store } from '@ngxs/store'; @@ -6,9 +7,16 @@ import { from, Observable, of } from 'rxjs'; import { filter, switchMap, tap } from 'rxjs/operators'; import { RestOccurError } from '../actions/rest.actions'; import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; +import { SessionStateService } from '../services/session-state.service'; import { ConfigStateService } from '../services/config-state.service'; import { EnvironmentService } from '../services/environment.service'; -import { RestService } from '../services/rest.service'; + +export interface LoginParams { + username: string; + password: string; + rememberMe?: boolean; + redirectUrl?: string; +} export const oAuthStorage = localStorage; @@ -21,9 +29,9 @@ export abstract class AuthFlowStrategy { protected oAuthService: OAuthService; protected oAuthConfig: AuthConfig; abstract checkIfInternalAuth(): boolean; - abstract login(): void; + abstract navigateToLogin(): void; abstract logout(): Observable; - abstract destroy(): void; + abstract login(params?: LoginParams): Observable; private catchError = err => this.store.dispatch(new RestOccurError(err)); @@ -54,8 +62,6 @@ export abstract class AuthFlowStrategy { }) .catch(this.catchError); } - - setRememberMe(remember: boolean) {} } export class AuthCodeFlowStrategy extends AuthFlowStrategy { @@ -68,7 +74,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { .then(() => this.oAuthService.setupAutomaticSilentRefresh({}, 'access_token')); } - login() { + navigateToLogin() { this.oAuthService.initCodeFlow(); } @@ -81,7 +87,10 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { return from(this.oAuthService.revokeTokenAndLogout()); } - destroy() {} + login() { + this.oAuthService.initCodeFlow(); + return of(null); + } } export class AuthPasswordFlowStrategy extends AuthFlowStrategy { @@ -112,11 +121,20 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { }); } - async init() { - this.oAuthService.events.pipe(filter(event => event.type === 'logout')).subscribe(() => { - this.removeRememberMe(); - }); + private setRememberMe(remember: boolean) { + this.removeRememberMe(); + localStorage.setItem(this.storageKey, 'true'); + document.cookie = `${this.cookieKey}=true${ + remember ? ';expires=Fri, 31 Dec 9999 23:59:59 GMT' : '' + }`; + } + private removeRememberMe() { + localStorage.removeItem(this.storageKey); + document.cookie = this.cookieKey + '= ; expires = Thu, 01 Jan 1970 00:00:00 GMT'; + } + + async init() { if (!getCookieValueByName('rememberMe') && localStorage.getItem(this.storageKey)) { this.oAuthService.logOut(); } @@ -124,7 +142,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { return super.init().then(() => this.listenToTokenExpiration()); } - login() { + navigateToLogin() { const router = this.injector.get(Router); router.navigateByUrl('/account/login'); } @@ -133,29 +151,38 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { return true; } - logout() { + login(params: LoginParams): Observable { + const sessionState = this.injector.get(SessionStateService); const router = this.injector.get(Router); - - return from(this.oAuthService.revokeTokenAndLogout()).pipe( + const tenant = sessionState.getTenant(); + + return from( + this.oAuthService.fetchTokenUsingPasswordFlow( + params.username, + params.password, + new HttpHeaders({ ...(tenant && tenant.id && { __tenant: tenant.id }) }), + ), + ).pipe( switchMap(() => this.appConfigService.get()), - tap(res => this.configState.setState(res)), - tap(() => router.navigateByUrl('/')), + tap(res => { + this.configState.setState(res); + this.setRememberMe(params.rememberMe); + router.navigate([params.redirectUrl || '/']); + }), ); } - destroy() {} - - setRememberMe(remember: boolean) { - this.removeRememberMe(); - localStorage.setItem(this.storageKey, 'true'); - document.cookie = `${this.cookieKey}=true${ - remember ? ';expires=Fri, 31 Dec 9999 23:59:59 GMT' : '' - }`; - } + logout() { + const router = this.injector.get(Router); - removeRememberMe() { - localStorage.removeItem(this.storageKey); - document.cookie = this.cookieKey + '= ; expires = Thu, 01 Jan 1970 00:00:00 GMT'; + return from(this.oAuthService.revokeTokenAndLogout()).pipe( + switchMap(() => this.appConfigService.get()), + tap(res => { + this.configState.setState(res); + router.navigateByUrl('/'); + this.removeRememberMe(); + }), + ); } } diff --git a/npm/ng-packs/packages/core/src/lib/strategies/index.ts b/npm/ng-packs/packages/core/src/lib/strategies/index.ts index d71f952307..2e621e7907 100644 --- a/npm/ng-packs/packages/core/src/lib/strategies/index.ts +++ b/npm/ng-packs/packages/core/src/lib/strategies/index.ts @@ -1,4 +1,3 @@ -export * from './auth-flow.strategy'; export * from './container.strategy'; export * from './content-security.strategy'; export * from './content.strategy'; diff --git a/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts index f34f503227..151e0ec639 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts @@ -21,12 +21,12 @@ describe('AuthGuard', () => { expect(guard.canActivate()).toBe(true); }); - it('should execute the initLogin method of the authService', () => { + it('should execute the navigateToLogin method of the authService', () => { const authService = spectator.inject(AuthService); spectator.inject(OAuthService).hasValidAccessToken.andReturn(false); - const initLoginSpy = jest.spyOn(authService, 'initLogin'); + const navigateToLoginSpy = jest.spyOn(authService, 'navigateToLogin'); expect(guard.canActivate()).toBe(false); - expect(initLoginSpy).toHaveBeenCalled(); + expect(navigateToLoginSpy).toHaveBeenCalled(); }); }); diff --git a/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts index 04cfcb2bf6..2c04e9075d 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts @@ -1,13 +1,12 @@ import { registerLocaleData } from '@angular/common'; import { Injector } from '@angular/core'; import { OAuthService } from 'angular-oauth2-oidc'; -import { isObservable, Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; import { ABP } from '../models/common'; import { Environment } from '../models/environment'; import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; import { CurrentTenantDto } from '../proxy/volo/abp/asp-net-core/mvc/multi-tenancy/models'; -import { AuthFlowInitializer } from '../services/auth-flow-initializer'; +import { AuthService } from '../services'; import { ConfigStateService } from '../services/config-state.service'; import { EnvironmentService } from '../services/environment.service'; import { SessionStateService } from '../services/session-state.service'; @@ -26,8 +25,7 @@ export function getInitialData(injector: Injector) { environmentService.setState(options.environment as Environment); await getRemoteEnv(injector, options.environment); await parseTenantFromUrl(injector); - const authFlowInit = injector.get(AuthFlowInitializer).init(); - await (isObservable(authFlowInit) ? authFlowInit.toPromise() : authFlowInit); + await injector.get(AuthService).init(); if (options.skipGetAppConfiguration) return; diff --git a/npm/ng-packs/packages/theme-basic/src/lib/components/nav-items/current-user.component.ts b/npm/ng-packs/packages/theme-basic/src/lib/components/nav-items/current-user.component.ts index 85ad94768f..54965c71e6 100644 --- a/npm/ng-packs/packages/theme-basic/src/lib/components/nav-items/current-user.component.ts +++ b/npm/ng-packs/packages/theme-basic/src/lib/components/nav-items/current-user.component.ts @@ -7,7 +7,7 @@ import { Observable } from 'rxjs'; // tslint:disable-next-line: component-max-inline-declarations template: ` - {{ + {{ 'AbpAccount::Login' | abpLocalization }} @@ -54,8 +54,8 @@ export class CurrentUserComponent { constructor(private authService: AuthService, private configState: ConfigStateService) {} - initLogin() { - this.authService.initLogin(); + navigateToLogin() { + this.authService.navigateToLogin(); } logout() { diff --git a/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts b/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts index 1835b80eef..44f0647d7f 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts @@ -257,7 +257,7 @@ export class ErrorHandler { } private navigateToLogin() { - this.injector.get(AuthService).initLogin(); + this.injector.get(AuthService).navigateToLogin(); } createErrorComponent(instance: Partial) { diff --git a/templates/app/angular/src/app/home/home.component.ts b/templates/app/angular/src/app/home/home.component.ts index 3790f8fe3f..c0b4602ee2 100644 --- a/templates/app/angular/src/app/home/home.component.ts +++ b/templates/app/angular/src/app/home/home.component.ts @@ -14,6 +14,6 @@ export class HomeComponent { constructor(private oAuthService: OAuthService, private authService: AuthService) {} login() { - this.authService.initLogin(); + this.authService.navigateToLogin(); } }