diff --git a/npm/ng-packs/packages/core/src/lib/services/session-state.service.ts b/npm/ng-packs/packages/core/src/lib/services/session-state.service.ts index d3bac9a338..36f7c9e94a 100644 --- a/npm/ng-packs/packages/core/src/lib/services/session-state.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/session-state.service.ts @@ -5,6 +5,7 @@ import { Session } from '../models/session'; import { CurrentTenantDto } from '../proxy/volo/abp/asp-net-core/mvc/multi-tenancy/models'; import { InternalStore } from '../utils/internal-store-utils'; import { ConfigStateService } from './config-state.service'; +import { AbpLocalStorageService } from './local-storage.service'; @Injectable({ providedIn: 'root', @@ -13,16 +14,19 @@ export class SessionStateService { private readonly store = new InternalStore({} as Session.State); private updateLocalStorage = () => { - localStorage.setItem('abpSession', JSON.stringify(this.store.state)); + this.localStorageService.setItem('abpSession', JSON.stringify(this.store.state)); }; - constructor(private configState: ConfigStateService) { + constructor( + private configState: ConfigStateService, + private localStorageService: AbpLocalStorageService, + ) { this.init(); this.setInitialLanguage(); } private init() { - const session = localStorage.getItem('abpSession'); + const session = this.localStorageService.getItem('abpSession'); if (session) { this.store.set(JSON.parse(session)); } diff --git a/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts b/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts index 5680b9582d..ac800e7c8f 100644 --- a/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts +++ b/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts @@ -2,14 +2,14 @@ import { APP_INITIALIZER, ModuleWithProviders, NgModule, Provider } from '@angul import { CommonModule } from '@angular/common'; import { OAuthModule, OAuthStorage } from 'angular-oauth2-oidc'; import { + AbpLocalStorageService, ApiInterceptor, AuthGuard, AuthService, CHECK_AUTHENTICATION_STATE_FN_KEY, noop, - PIPE_TO_LOGIN_FN_KEY + PIPE_TO_LOGIN_FN_KEY, } from '@abp/ng.core'; -import { storageFactory } from './utils/storage.factory'; import { AbpOAuthService } from './services'; import { OAuthConfigurationHandler } from './handlers/oauth-configuration.handler'; import { HTTP_INTERCEPTORS } from '@angular/common/http'; @@ -59,7 +59,7 @@ export class AbpOAuthModule { useFactory: noop, }, OAuthModule.forRoot().providers as Provider[], - { provide: OAuthStorage, useFactory: storageFactory }, + { provide: OAuthStorage, useClass: AbpLocalStorageService }, ], }; } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts index 7a07cb8985..c1bf50e0b5 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts @@ -9,6 +9,7 @@ import { import { Observable, of } from 'rxjs'; import { filter, switchMap, tap } from 'rxjs/operators'; import { + AbpLocalStorageService, ConfigStateService, EnvironmentService, HttpErrorReporterService, @@ -29,6 +30,7 @@ export abstract class AuthFlowStrategy { protected oAuthService: OAuthService2; protected oAuthConfig!: AuthConfig; protected sessionState: SessionStateService; + protected localStorageService: AbpLocalStorageService; protected tenantKey: string; abstract checkIfInternalAuth(queryParams?: Params): boolean; @@ -50,6 +52,7 @@ export abstract class AuthFlowStrategy { this.configState = injector.get(ConfigStateService); this.oAuthService = injector.get(OAuthService2); this.sessionState = injector.get(SessionStateService); + this.localStorageService = injector.get(AbpLocalStorageService); this.oAuthConfig = this.environment.getEnvironment().oAuthConfig || {}; this.tenantKey = injector.get(TENANT_KEY); diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts index 2e2e278985..fab747fe90 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts @@ -4,7 +4,7 @@ import { Params, Router } from '@angular/router'; import { from, Observable, pipe } from 'rxjs'; import { HttpHeaders } from '@angular/common/http'; import { AuthFlowStrategy } from './auth-flow-strategy'; -import { pipeToLogin, removeRememberMe, setRememberMe } from '../utils/auth-utils'; +import { pipeToLogin, removeRememberMe } from '../utils/auth-utils'; import { LoginParams } from '@abp/ng.core'; import { clearOAuthStorage } from '../utils/clear-o-auth-storage'; @@ -33,7 +33,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { this.refreshToken(); } else { this.oAuthService.logOut(); - removeRememberMe(); + removeRememberMe(this.localStorageService); this.configState.refreshAppState().subscribe(); } }); @@ -74,7 +74,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { switchMap(() => this.configState.refreshAppState()), tap(() => { router.navigateByUrl('/'); - removeRememberMe(); + removeRememberMe(this.localStorageService); }), ); } @@ -82,7 +82,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { protected refreshToken() { return this.oAuthService.refreshToken().catch(() => { clearOAuthStorage(); - removeRememberMe(); + removeRememberMe(this.localStorageService); }); } } diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts index 24f94fe129..f1dc5b6106 100644 --- a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts +++ b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts @@ -7,7 +7,7 @@ import { ConfigStateService, LoginParams, PipeToLoginFn, - SetTokenResponseToStorageFn, + AbpLocalStorageService, } from '@abp/ng.core'; const cookieKey = 'rememberMe'; @@ -19,25 +19,28 @@ export const pipeToLogin: PipeToLoginFn = function ( ) { const configState = injector.get(ConfigStateService); const router = injector.get(Router); - + const localStorage = injector.get(AbpLocalStorageService); return pipe( switchMap(() => configState.refreshAppState()), tap(() => { - setRememberMe(params.rememberMe); + setRememberMe(params.rememberMe, localStorage); if (params.redirectUrl) router.navigate([params.redirectUrl]); }), ); }; -export function setRememberMe(remember: boolean | undefined) { - removeRememberMe(); - localStorage.setItem(storageKey, 'true'); +export function setRememberMe( + remember: boolean | undefined, + localStorageService: AbpLocalStorageService, +) { + removeRememberMe(localStorageService); + localStorageService.setItem(storageKey, 'true'); document.cookie = `${cookieKey}=true; path=/${ remember ? ' ;expires=Fri, 31 Dec 9999 23:59:59 GMT' : '' }`; } -export function removeRememberMe() { - localStorage.removeItem(storageKey); +export function removeRememberMe(localStorageService: AbpLocalStorageService) { + localStorageService.removeItem(storageKey); document.cookie = cookieKey + '= ; path=/; expires = Thu, 01 Jan 1970 00:00:00 GMT'; }