Skip to content

Commit 6807b65

Browse files
atarix83Andrea Barbasso
authored andcommitted
[DSC-2142] throw error when xsrf is not available
1 parent 2500195 commit 6807b65

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/app/core/auth/auth-request.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Observable } from 'rxjs';
2-
import { distinctUntilChanged, filter, map, switchMap, tap, take } from 'rxjs/operators';
1+
import { Observable, of } from 'rxjs';
2+
import { distinctUntilChanged, filter, map, switchMap, tap, take, catchError } from 'rxjs/operators';
33
import { HALEndpointService } from '../shared/hal-endpoint.service';
44
import { RequestService } from '../data/request.service';
55
import { isNotEmpty, isNotEmptyOperator } from '../../shared/empty.util';
@@ -129,6 +129,10 @@ export abstract class AuthRequestService {
129129
} else {
130130
return null;
131131
}
132+
}),
133+
catchError((error: Error) => {
134+
console.error(error?.message);
135+
return of(null);
132136
})
133137
);
134138
}

src/app/core/auth/server-auth-request.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
XSRF_RESPONSE_HEADER,
1515
DSPACE_XSRF_COOKIE
1616
} from '../xsrf/xsrf.constants';
17-
import { map, tap } from 'rxjs/operators';
17+
import { map } from 'rxjs/operators';
1818
import { Observable } from 'rxjs';
1919
import { XSRFService } from '../xsrf/xsrf.service';
2020

@@ -45,7 +45,13 @@ export class ServerAuthRequestService extends AuthRequestService {
4545
return this.httpClient.get(this.halService.getRootHref(), { observe: 'response' }).pipe(
4646
// retrieve the XSRF token from the response header
4747
map((response: HttpResponse<any>) => response.headers.get(XSRF_RESPONSE_HEADER)),
48-
tap(() => this.xsrfService.tokenInitialized$.next(true)),
48+
map((xsrfToken: string) => {
49+
if (!xsrfToken) {
50+
throw new Error('Failed to initialize XSRF token');
51+
}
52+
this.xsrfService.tokenInitialized$.next(true);
53+
return xsrfToken;
54+
}),
4955
// Use that token to create an HttpHeaders object
5056
map((xsrfToken: string) => new HttpHeaders()
5157
.set('Content-Type', 'application/json; charset=utf-8')

0 commit comments

Comments
 (0)