Browse Source

auth code flow strategy reactored to support ssr

feature/ssr-migration-schematic
erdemcaygor 1 year ago
parent
commit
77855e251b
  1. 77
      npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts

77
npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts

@ -1,11 +1,14 @@
import { Inject, PLATFORM_ID } from '@angular/core';
import { noop } from '@abp/ng.core'; import { noop } from '@abp/ng.core';
import { Params } from '@angular/router'; import { Params } from '@angular/router';
import { filter, from, of, take, tap } from 'rxjs'; import { filter, from, of, take, tap } from 'rxjs';
import { AuthFlowStrategy } from './auth-flow-strategy'; import { AuthFlowStrategy } from './auth-flow-strategy';
import { isTokenExpired } from '../utils'; import { isTokenExpired } from '../utils';
import { isPlatformBrowser } from '@angular/common';
export class AuthCodeFlowStrategy extends AuthFlowStrategy { export class AuthCodeFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = false; readonly isInternalAuth = false;
@Inject(PLATFORM_ID) platformId: string;
async init() { async init() {
this.checkRememberMeOption(); this.checkRememberMeOption();
@ -42,51 +45,61 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
} }
protected setUICulture() { protected setUICulture() {
const urlParams = new URLSearchParams(window.location.search); if (isPlatformBrowser(this.platformId)) {
this.configState.uiCultureFromAuthCodeFlow = urlParams.get('ui-culture'); const urlParams = new URLSearchParams(window.location.search);
this.configState.uiCultureFromAuthCodeFlow = urlParams.get('ui-culture');
}
} }
protected replaceURLParams() { protected replaceURLParams() {
const location = this.windowService.window.location; if (isPlatformBrowser(this.platformId)) {
const history = this.windowService.window.history; const location = this.windowService.window.location;
const history = this.windowService.window.history;
const query = location.search const query = location.search
.replace(/([?&])iss=[^&]*&?/, '$1') .replace(/([?&])iss=[^&]*&?/, '$1')
.replace(/([?&])culture=[^&]*&?/, '$1') .replace(/([?&])culture=[^&]*&?/, '$1')
.replace(/([?&])ui-culture=[^&]*&?/, '$1') .replace(/([?&])ui-culture=[^&]*&?/, '$1')
.replace(/[?&]+$/, ''); .replace(/[?&]+$/, '');
const href = location.origin + location.pathname + query + location.hash; const href = location.origin + location.pathname + query + location.hash;
history.replaceState(null, '', href); history.replaceState(null, '', href);
}
} }
protected listenToTokenReceived() { protected listenToTokenReceived() {
this.oAuthService.events if (isPlatformBrowser(this.platformId)) {
.pipe( this.oAuthService.events
filter(event => event.type === 'token_received'), .pipe(
tap(() => { filter(event => event.type === 'token_received'),
this.setUICulture(); tap(() => {
this.replaceURLParams(); this.setUICulture();
}), this.replaceURLParams();
take(1), }),
) take(1),
.subscribe(); )
.subscribe();
}
} }
navigateToLogin(queryParams?: Params) { navigateToLogin(queryParams?: Params) {
let additionalState = ''; if (isPlatformBrowser(this.platformId)) {
if (queryParams?.returnUrl) { let additionalState = '';
additionalState = queryParams.returnUrl; if (queryParams?.returnUrl) {
additionalState = queryParams.returnUrl;
}
const cultureParams = this.getCultureParams(queryParams);
this.oAuthService.initCodeFlow(additionalState, cultureParams);
} }
const cultureParams = this.getCultureParams(queryParams);
this.oAuthService.initCodeFlow(additionalState, cultureParams);
} }
checkIfInternalAuth(queryParams?: Params) { checkIfInternalAuth(queryParams?: Params) {
this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams)); if (isPlatformBrowser(this.platformId)) {
return false; this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams));
return false;
}
} }
logout(queryParams?: Params) { logout(queryParams?: Params) {
@ -99,7 +112,9 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
} }
login(queryParams?: Params) { login(queryParams?: Params) {
this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams)); if (isPlatformBrowser(this.platformId)) {
return of(null); this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams));
return of(null);
}
} }
} }

Loading…
Cancel
Save