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

Refresh v2 #38

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

retry fetch properly if recieved 401

  • Loading branch information
fcjr committed Dec 16, 2020
commit c49a4b2e886619f7ccaf795ea6c27f813f9c5461
@@ -215,16 +215,14 @@ class TokenPool {
pretokens.push({ token, blindFactor });
}

const response = await fetch(`${API_BASE_URL}/tokens/new`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
blindTokens,
}),
});
let response = await this._fetchNewTokens(accessToken, blindTokens);
if (response.status === 401) {
// try to refresh token and try again if authorization failed
// as the token technically could have expired by the time the request
// arives
const accessToken = await AccessToken.get();
response = await this._fetchNewTokens(accessToken, blindTokens);
}
if (response.ok) {
const { tokens } = await response.json();
const res = [];
@@ -246,9 +244,19 @@ class TokenPool {
}));
console.warn(`Adding ${res.length} tokens to acquired pool`);
this.tokens.push(...res);
} else if (response.status === 401) {
// refresh the access token. This will call generateTokens if the refresh is successful
AccessToken.refresh();
}
}

async _fetchNewTokens(accessToken, blindTokens) {
return fetch(`${API_BASE_URL}/tokens/new`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
blindTokens,
}),
});
}
}
ProTip! Use n and p to navigate between commits in a pull request.