Skip to content

Commit

Permalink
update : seperation of logical functions, abpwindowservice usage
Browse files Browse the repository at this point in the history
  • Loading branch information
erhanilze committed Oct 3, 2024
1 parent dca0a08 commit 652e49b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class ConfigStateService {

return this.getlocalizationResource(cultureName).pipe(
map(result => ({ ...appState, localization: { ...appState.localization, ...result } })),
tap(() => (this.uiCultureFromAuthCodeFlow = undefined)),
);
}

Expand Down
6 changes: 3 additions & 3 deletions npm/ng-packs/packages/core/src/lib/services/window.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { DOCUMENT } from '@angular/common';

@Injectable({ providedIn: 'root' })
export class AbpWindowService {
protected readonly document = inject(DOCUMENT);
protected readonly window = this.document.defaultView;
protected readonly navigator = this.window.navigator;
public readonly document = inject(DOCUMENT);
public readonly window = this.document.defaultView;
public readonly navigator = this.window.navigator;

copyToClipboard(text: string): Promise<void> {
return this.navigator.clipboard.writeText(text);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { noop } from '@abp/ng.core';
import { Params } from '@angular/router';
import { filter, from, of, tap } from 'rxjs';
import { filter, from, of, take, tap } from 'rxjs';
import { AuthFlowStrategy } from './auth-flow-strategy';
import { isTokenExpired } from '../utils';

Expand All @@ -9,7 +9,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {

async init() {
this.checkRememberMeOption();
this.setUICulture();
this.listenToTokenReceived();

return super
.init()
Expand All @@ -35,6 +35,46 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
}
}

private getCultureParams(queryParams?: Params) {
const lang = this.sessionState.getLanguage();
const culture = { culture: lang, 'ui-culture': lang };
return { ...(lang && culture), ...queryParams };
}

protected setUICulture() {
const urlParams = new URLSearchParams(window.location.search);
this.configState.uiCultureFromAuthCodeFlow = urlParams.get('ui-culture');
}

protected replaceURLParams() {
const location = this.windowService.window.location;
const history = this.windowService.window.history;

const href =
location.origin +
location.pathname +
location.search
.replace(/iss=[^&$]*/, '')
.replace(/culture=[^&$]*/, '')
.replace(/ui-culture=[^&$]*/, '') +
location.hash;

history.replaceState(null, '', href);
}

protected listenToTokenReceived() {
this.oAuthService.events
.pipe(
filter(event => event.type === 'token_received'),
tap(() => {
this.setUICulture();
this.replaceURLParams();
}),
take(1),
)
.subscribe();
}

navigateToLogin(queryParams?: Params) {
let additionalState = '';
if (queryParams?.returnUrl) {
Expand Down Expand Up @@ -63,23 +103,4 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams));
return of(null);
}

private getCultureParams(queryParams?: Params) {
const lang = this.sessionState.getLanguage();
const culture = { culture: lang, 'ui-culture': lang };
return { ...(lang && culture), ...queryParams };
}

private setUICulture() {
this.oAuthService.events
.pipe(
filter(event => event.type === 'token_received'),
tap(e => {
const urlParams = new URLSearchParams(window.location.search);
this.configState.uiCultureFromAuthCodeFlow = urlParams.get('ui-culture');
window.history.replaceState(null, '', window.location.pathname);
}),
)
.subscribe();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {

import {
AbpLocalStorageService,
AbpWindowService,
ConfigStateService,
EnvironmentService,
HttpErrorReporterService,
Expand All @@ -38,6 +39,7 @@ export abstract class AuthFlowStrategy {
protected sessionState: SessionStateService;
protected localStorageService: AbpLocalStorageService;
protected rememberMeService: RememberMeService;
protected windowService: AbpWindowService;
protected tenantKey: string;
protected router: Router;

Expand Down Expand Up @@ -65,6 +67,7 @@ export abstract class AuthFlowStrategy {
this.router = injector.get(Router);
this.oAuthErrorFilterService = injector.get(OAuthErrorFilterService);
this.rememberMeService = injector.get(RememberMeService);
this.windowService = injector.get(AbpWindowService);

this.listenToOauthErrors();
}
Expand Down

0 comments on commit 652e49b

Please sign in to comment.