Skip to content

Commit

Permalink
Retry CSRF if first one failed
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmalloy committed Apr 3, 2024
1 parent 5aeccc6 commit cc4a02b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/app/component/login-popup/login-popup.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@if (config.login) {
<div class="fake-link no-select" (click)="doLogin()" i18n>Click here to login.</div>
} @else {
Please log in and try again.
<div>Please log in and try again.</div>
}
<button type="button" (click)="clear()" i18n>clear</button>
}
Expand Down
15 changes: 13 additions & 2 deletions src/app/http/csrf.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable, isDevMode } from '@angular/core';
import { Observable } from 'rxjs';
import { catchError, filter, Observable, retry, retryWhen, switchMap, throwError } from 'rxjs';
import { ConfigService } from '../service/config.service';

@Injectable()
Expand All @@ -25,6 +25,17 @@ export class CsrfInterceptor implements HttpInterceptor {
headers: request.headers.set('X-XSRF-TOKEN', this.getCsrfToken()),
withCredentials: this.withCredentials,
});
return next.handle(modifiedReq);
return next.handle(modifiedReq).pipe(
catchError(err => {
if (!err.status || err.status === 403) {
const retryReq = request.clone({
headers: request.headers.set('X-XSRF-TOKEN', this.getCsrfToken()),
withCredentials: this.withCredentials,
});
return next.handle(retryReq);
}
return throwError(() => err);
}),
);
}
}

0 comments on commit cc4a02b

Please sign in to comment.