Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeat request in error interceptor sends old header #145

Closed
faradoxuz opened this issue Aug 1, 2018 · 2 comments
Closed

Repeat request in error interceptor sends old header #145

faradoxuz opened this issue Aug 1, 2018 · 2 comments

Comments

@faradoxuz
Copy link

faradoxuz commented Aug 1, 2018

Hello, I am using restangular library in my application
in error addErrorInterceptor config I want to repeat request if user access token expires after refreshing the token. here is piece of my code:

.addErrorInterceptor((response, subject, responseHandler) => {
            if (response.status === 401) {
                authService.refreshAccesstoken()
                    .pipe(
                        switchMap(refreshAccesstokenResponse => {
                            console.log(refreshAccesstokenResponse) // this is new token
                            response.request.headers.set('Authorization', `Bearer  ${refreshAccesstokenResponse}`)
                            return response.repeatRequest(response.request.clone());
                        })
                    )
                    .subscribe(
                        res => {
                            console.log(res);
                            responseHandler(res)
                        },
                        err => subject.error(err)
                    );
                return false; // error handled
            }

after setting headers, they are not updating, it still send old headers

@leoxgnr93
Copy link

Hi,

You must update the headers cloning the headers and then just repeat the request

        const newHeaders = new HttpHeaders({ 'Authorization': BEARER_KEYWORD + authService.getAccessToken() });
        const newRequest = response.request.clone({ headers: newHeaders });
            return response.repeatRequest(newRequest);

It works for me, hope for you too

@rshchpkn
Copy link
Collaborator

Hi @faradoxuz, @leoxgnr93 code sample should work. Updated readme file in #177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants