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

Retry failed request after RefreshToken #23

Open
digitalkaoz opened this issue Jun 25, 2021 · 2 comments
Open

Retry failed request after RefreshToken #23

digitalkaoz opened this issue Jun 25, 2021 · 2 comments

Comments

@digitalkaoz
Copy link
Contributor

it would be nice to have a general retry handler aswell:

const onOk = (response) => response;
const onError = (error) => {
	// Return any error which is not due to authentication back to the calling service
	if (error && error.response && error.response.status !== 401) {
		return Promise.reject(error);
	}

	const config = error.config;

	// Logout user if token refresh didn't work or user is disabled
	if (error.config.url.endsWith("auth/refresh")) {
		logout();
		return Promise.reject(error);
	}

	// Try request again with new token
	return new Promise((resolve, reject) => {
		requestRefresh().then((response) => {
			setAuthTokens(response.data)
			return http.request(config).then((response) => resolve(response));
		}).catch((e) => {
			clearAuthTokens()
			reject(e)
		})
	});
};

http.interceptors.response.use(onOk, onError);
@mvanroon
Copy link
Contributor

@digitalkaoz

This is already the case:

  • A request will check beforehand if the access token needs to be refreshed first
  • Any requests are put in a queue while the tokens are being refreshed
  • When the tokens have successfully been refreshed, the queue is resolved with the new access token

Is this what you mean?

@digitalkaoz
Copy link
Contributor Author

If yes, it didnt worked as expected in my case, but good that it seems to be implemented

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

2 participants