mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
force initservices to wait until authentication is no longer blocking
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { distinctUntilChanged, filter, map, mergeMap, switchMap, tap } from 'rxjs/operators';
|
import { distinctUntilChanged, filter, map, mergeMap, switchMap, tap, take } from 'rxjs/operators';
|
||||||
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
||||||
import { RequestService } from '../data/request.service';
|
import { RequestService } from '../data/request.service';
|
||||||
import { isNotEmpty } from '../../shared/empty.util';
|
import { isNotEmpty } from '../../shared/empty.util';
|
||||||
@@ -26,8 +26,8 @@ export abstract class AuthRequestService {
|
|||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fetchRequest(request: RestRequest): Observable<RemoteData<AuthStatus>> {
|
protected fetchRequest(requestId: string): Observable<RemoteData<AuthStatus>> {
|
||||||
return this.rdbService.buildFromRequestUUID<AuthStatus>(request.uuid).pipe(
|
return this.rdbService.buildFromRequestUUID<AuthStatus>(requestId).pipe(
|
||||||
getFirstCompletedRemoteData(),
|
getFirstCompletedRemoteData(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -37,27 +37,36 @@ export abstract class AuthRequestService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public postToEndpoint(method: string, body?: any, options?: HttpOptions): Observable<RemoteData<AuthStatus>> {
|
public postToEndpoint(method: string, body?: any, options?: HttpOptions): Observable<RemoteData<AuthStatus>> {
|
||||||
return this.halService.getEndpoint(this.linkName).pipe(
|
const requestId = this.requestService.generateRequestId();
|
||||||
|
|
||||||
|
this.halService.getEndpoint(this.linkName).pipe(
|
||||||
filter((href: string) => isNotEmpty(href)),
|
filter((href: string) => isNotEmpty(href)),
|
||||||
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
||||||
distinctUntilChanged(),
|
distinctUntilChanged(),
|
||||||
map((endpointURL: string) => new PostRequest(this.requestService.generateRequestId(), endpointURL, body, options)),
|
map((endpointURL: string) => new PostRequest(requestId, endpointURL, body, options)),
|
||||||
tap((request: PostRequest) => this.requestService.send(request)),
|
take(1)
|
||||||
mergeMap((request: PostRequest) => this.fetchRequest(request)),
|
).subscribe((request: PostRequest) => {
|
||||||
distinctUntilChanged());
|
this.requestService.send(request);
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.fetchRequest(requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getRequest(method: string, options?: HttpOptions): Observable<RemoteData<AuthStatus>> {
|
public getRequest(method: string, options?: HttpOptions): Observable<RemoteData<AuthStatus>> {
|
||||||
return this.halService.getEndpoint(this.linkName).pipe(
|
const requestId = this.requestService.generateRequestId();
|
||||||
|
|
||||||
|
this.halService.getEndpoint(this.linkName).pipe(
|
||||||
filter((href: string) => isNotEmpty(href)),
|
filter((href: string) => isNotEmpty(href)),
|
||||||
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
||||||
distinctUntilChanged(),
|
distinctUntilChanged(),
|
||||||
map((endpointURL: string) => new GetRequest(this.requestService.generateRequestId(), endpointURL, undefined, options)),
|
map((endpointURL: string) => new GetRequest(requestId, endpointURL, undefined, options)),
|
||||||
tap((request: GetRequest) => this.requestService.send(request)),
|
take(1)
|
||||||
mergeMap((request: GetRequest) => this.fetchRequest(request)),
|
).subscribe((request: GetRequest) => {
|
||||||
distinctUntilChanged());
|
this.requestService.send(request);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
return this.fetchRequest(requestId);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Factory function to create the request object to send. This needs to be a POST client side and
|
* Factory function to create the request object to send. This needs to be a POST client side and
|
||||||
* a GET server side. Due to CSRF validation, the server isn't allowed to send a POST, so we allow
|
* a GET server side. Due to CSRF validation, the server isn't allowed to send a POST, so we allow
|
||||||
|
@@ -17,6 +17,7 @@ import {
|
|||||||
import { AuthTokenInfo } from './models/auth-token-info.model';
|
import { AuthTokenInfo } from './models/auth-token-info.model';
|
||||||
import { AuthMethod } from './models/auth.method';
|
import { AuthMethod } from './models/auth.method';
|
||||||
import { AuthMethodType } from './models/auth.method-type';
|
import { AuthMethodType } from './models/auth.method-type';
|
||||||
|
import { StoreActionTypes } from '../../store.actions';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The auth state.
|
* The auth state.
|
||||||
@@ -251,6 +252,11 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
|||||||
idle: false,
|
idle: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
case StoreActionTypes.REHYDRATE:
|
||||||
|
return Object.assign({}, state, {
|
||||||
|
blocking: true,
|
||||||
|
});
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
@@ -91,6 +91,13 @@ export class BrowserInitService extends InitService {
|
|||||||
|
|
||||||
this.initKlaro();
|
this.initKlaro();
|
||||||
|
|
||||||
|
// wait for auth to be ready
|
||||||
|
await this.store.pipe(
|
||||||
|
select(isAuthenticationBlocking),
|
||||||
|
distinctUntilChanged(),
|
||||||
|
find((b: boolean) => b === false)
|
||||||
|
).toPromise();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
* http://www.dspace.org/license/
|
* http://www.dspace.org/license/
|
||||||
*/
|
*/
|
||||||
import { InitService } from '../../app/init.service';
|
import { InitService } from '../../app/init.service';
|
||||||
import { Store } from '@ngrx/store';
|
import { Store, select } from '@ngrx/store';
|
||||||
import { AppState } from '../../app/app.reducer';
|
import { AppState } from '../../app/app.reducer';
|
||||||
import { TransferState } from '@angular/platform-browser';
|
import { TransferState } from '@angular/platform-browser';
|
||||||
import { CorrelationIdService } from '../../app/correlation-id/correlation-id.service';
|
import { CorrelationIdService } from '../../app/correlation-id/correlation-id.service';
|
||||||
@@ -20,7 +20,8 @@ import { MetadataService } from '../../app/core/metadata/metadata.service';
|
|||||||
import { BreadcrumbsService } from '../../app/breadcrumbs/breadcrumbs.service';
|
import { BreadcrumbsService } from '../../app/breadcrumbs/breadcrumbs.service';
|
||||||
import { CSSVariableService } from '../../app/shared/sass-helper/sass-helper.service';
|
import { CSSVariableService } from '../../app/shared/sass-helper/sass-helper.service';
|
||||||
import { ThemeService } from '../../app/shared/theme-support/theme.service';
|
import { ThemeService } from '../../app/shared/theme-support/theme.service';
|
||||||
import { take } from 'rxjs/operators';
|
import { take, distinctUntilChanged, find } from 'rxjs/operators';
|
||||||
|
import { isAuthenticationBlocking } from '../../app/core/auth/selectors';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs server-side initialization.
|
* Performs server-side initialization.
|
||||||
@@ -66,6 +67,13 @@ export class ServerInitService extends InitService {
|
|||||||
this.initRouteListeners();
|
this.initRouteListeners();
|
||||||
this.themeService.listenForThemeChanges(false);
|
this.themeService.listenForThemeChanges(false);
|
||||||
|
|
||||||
|
// wait for auth to be ready
|
||||||
|
await this.store.pipe(
|
||||||
|
select(isAuthenticationBlocking),
|
||||||
|
distinctUntilChanged(),
|
||||||
|
find((b: boolean) => b === false)
|
||||||
|
).toPromise();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user