@@ -13,6 +13,7 @@ import {
1313 map ,
1414 mergeMap ,
1515 take ,
16+ withLatestFrom ,
1617} from 'rxjs/operators' ;
1718
1819import {
@@ -25,6 +26,7 @@ import { ParsedResponse } from '../cache/response.models';
2526import { DSpaceSerializer } from '../dspace-rest/dspace.serializer' ;
2627import { DspaceRestService } from '../dspace-rest/dspace-rest.service' ;
2728import { RawRestResponse } from '../dspace-rest/raw-rest-response.model' ;
29+ import { XSRFService } from '../xsrf/xsrf.service' ;
2830import {
2931 RequestActionTypes ,
3032 RequestErrorAction ,
@@ -35,6 +37,7 @@ import {
3537import { RequestService } from './request.service' ;
3638import { RequestEntry } from './request-entry.model' ;
3739import { RequestError } from './request-error.model' ;
40+ import { RestRequestMethod } from './rest-request-method' ;
3841import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model' ;
3942
4043@Injectable ( )
@@ -48,7 +51,11 @@ export class RequestEffects {
4851 ) ;
4952 } ) ,
5053 filter ( ( entry : RequestEntry ) => hasValue ( entry ) ) ,
51- map ( ( entry : RequestEntry ) => entry . request ) ,
54+ withLatestFrom ( this . xsrfService . tokenInitialized$ ) ,
55+ // If it's a GET request, or we have an XSRF token, dispatch it immediately
56+ // Otherwise wait for the XSRF token first
57+ filter ( ( [ entry , tokenInitialized ] : [ RequestEntry , boolean ] ) => entry . request . method === RestRequestMethod . GET || tokenInitialized === true ) ,
58+ map ( ( [ entry , tokenInitialized ] : [ RequestEntry , boolean ] ) => entry . request ) ,
5259 mergeMap ( ( request : RestRequestWithResponseParser ) => {
5360 let body = request . body ;
5461 if ( isNotEmpty ( request . body ) && ! request . isMultipart ) {
@@ -89,6 +96,7 @@ export class RequestEffects {
8996 private restApi : DspaceRestService ,
9097 private injector : Injector ,
9198 protected requestService : RequestService ,
99+ protected xsrfService : XSRFService ,
92100 ) { }
93101
94102}
0 commit comments