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 6ea1aee90d..fbd2206fd8 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 @@ -20,13 +20,17 @@ export class AuthService { } private setStrategy = () => { - const flow = this.environment.getEnvironment().oAuthConfig.responseType || 'password'; + const flow = + this.environment.getEnvironment().oAuthConfig.responseType === 'code' ? 'code' : 'password'; if (this.flow === flow) return; if (this.strategy) this.strategy.destroy(); this.flow = flow; - this.strategy = AUTH_FLOW_STRATEGY.Code(this.injector); + this.strategy = + flow === 'code' + ? AUTH_FLOW_STRATEGY.Code(this.injector) + : AUTH_FLOW_STRATEGY.Password(this.injector); }; private listenToSetEnvironment() { 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 d3d4c9f70f..d6aceece58 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,10 +1,14 @@ import { Injector } from '@angular/core'; +import { Router } from '@angular/router'; import { Store } from '@ngxs/store'; import { AuthConfig, OAuthService, OAuthStorage } from 'angular-oauth2-oidc'; -import { Observable, of } from 'rxjs'; +import { from, Observable, of } from 'rxjs'; +import { 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 { ConfigStateService } from '../services/config-state.service'; import { EnvironmentService } from '../services/environment.service'; +import { RestService } from '../services/rest.service'; export const oAuthStorage = localStorage; @@ -70,9 +74,43 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { } logout() { - this.oAuthService.revokeTokenAndLogout(); - // TODO: no need to return of(null). It may be removed in v5.0. - return of(null); + return from(this.oAuthService.revokeTokenAndLogout()); + } + + destroy() {} +} + +export class AuthPasswordFlowStrategy extends AuthFlowStrategy { + readonly isInternalAuth = true; + private appConfigService = this.injector.get(AbpApplicationConfigurationService); + + login() { + const router = this.injector.get(Router); + router.navigateByUrl('/account/login'); + } + + checkIfInternalAuth() { + return true; + } + + logout() { + const rest = this.injector.get(RestService); + + const issuer = this.configState.getDeep('environment.oAuthConfig.issuer'); + return rest + .request( + { + method: 'GET', + url: '/api/account/logout', + }, + null, + issuer, + ) + .pipe( + switchMap(() => from(this.oAuthService.revokeTokenAndLogout())), + switchMap(() => this.appConfigService.get()), + tap(res => this.configState.setState(res)), + ); } destroy() {} @@ -82,6 +120,9 @@ export const AUTH_FLOW_STRATEGY = { Code(injector: Injector) { return new AuthCodeFlowStrategy(injector); }, + Password(injector: Injector) { + return new AuthPasswordFlowStrategy(injector); + }, }; export function clearOAuthStorage(storage: OAuthStorage = oAuthStorage) {