11import { Injectable , Injector } from '@angular/core' ;
22
33import { Actions , createEffect , ofType } from '@ngrx/effects' ;
4- import { catchError , filter , map , mergeMap , take } from 'rxjs/operators' ;
4+ import { catchError , filter , map , mergeMap , take , withLatestFrom } from 'rxjs/operators' ;
55
66import { hasValue , isNotEmpty } from '../../shared/empty.util' ;
77import { StoreActionTypes } from '../../store.actions' ;
88import { getClassForType } from '../cache/builders/build-decorators' ;
99import { RawRestResponse } from '../dspace-rest/raw-rest-response.model' ;
1010import { DspaceRestService } from '../dspace-rest/dspace-rest.service' ;
1111import { DSpaceSerializer } from '../dspace-rest/dspace.serializer' ;
12+ import { XSRFService } from '../xsrf/xsrf.service' ;
1213import {
1314 RequestActionTypes ,
1415 RequestErrorAction ,
@@ -19,6 +20,7 @@ import {
1920import { RequestService } from './request.service' ;
2021import { ParsedResponse } from '../cache/response.models' ;
2122import { RequestError } from './request-error.model' ;
23+ import { RestRequestMethod } from './rest-request-method' ;
2224import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model' ;
2325import { RequestEntry } from './request-entry.model' ;
2426
@@ -33,7 +35,11 @@ export class RequestEffects {
3335 ) ;
3436 } ) ,
3537 filter ( ( entry : RequestEntry ) => hasValue ( entry ) ) ,
36- map ( ( entry : RequestEntry ) => entry . request ) ,
38+ withLatestFrom ( this . xsrfService . tokenInitialized$ ) ,
39+ // If it's a GET request, or we have an XSRF token, dispatch it immediately
40+ // Otherwise wait for the XSRF token first
41+ filter ( ( [ entry , tokenInitialized ] : [ RequestEntry , boolean ] ) => entry . request . method === RestRequestMethod . GET || tokenInitialized === true ) ,
42+ map ( ( [ entry , tokenInitialized ] : [ RequestEntry , boolean ] ) => entry . request ) ,
3743 mergeMap ( ( request : RestRequestWithResponseParser ) => {
3844 let body = request . body ;
3945 if ( isNotEmpty ( request . body ) && ! request . isMultipart ) {
@@ -73,7 +79,8 @@ export class RequestEffects {
7379 private actions$ : Actions ,
7480 private restApi : DspaceRestService ,
7581 private injector : Injector ,
76- protected requestService : RequestService
82+ protected requestService : RequestService ,
83+ protected xsrfService : XSRFService ,
7784 ) { }
7885
7986}
0 commit comments