Skip to content

Commit

Permalink
Workaround of GMOD#3194, when not in web worker. Now refreshing page …
Browse files Browse the repository at this point in the history
…does help when there was a token present but refresh token was not found.
  • Loading branch information
grzelaka committed Jan 18, 2023
1 parent 0c2342d commit 02acc70
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export const InternetAccount = types
self.removeToken()
throw error
}
self.storeToken(validatedToken);
return {
internetAccountType: self.type,
authInfo: { token: validatedToken, configuration: getConf(self) },
Expand All @@ -241,6 +242,9 @@ export const InternetAccount = types
*/
getFetcher(loc?: UriLocation) {
return async (input: RequestInfo, init?: RequestInit) => {
if(!inWebWorker && loc){
loc.internetAccountPreAuthorization = await self.getPreAuthorizationInformation(loc);
}
const authToken = await self.getToken(loc)
const newInit = self.addAuthHeaderToInit(init, authToken)
return fetch(input, newInit)
Expand Down
10 changes: 8 additions & 2 deletions plugins/authentication/src/OAuthModel/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,12 @@ const stateModelFactory = (configSchema: OAuthInternetAccountConfigModel) => {
const refreshToken =
self.hasRefreshToken && self.retrieveRefreshToken()
if (refreshToken) {
resolve(await self.exchangeRefreshForAccessToken(refreshToken))
try {
const token = await self.exchangeRefreshForAccessToken(refreshToken);
resolve(token);
} catch (err) {
reject(new Error(`Token could not be refreshed. ${err} Please reload the page.`));
}
}
this.addMessageChannel(resolve, reject)
// may want to improve handling
Expand All @@ -346,9 +351,10 @@ const stateModelFactory = (configSchema: OAuthInternetAccountConfigModel) => {
const newToken = await refreshTokenPromise
return this.validateToken(newToken, location)
} catch (err) {
throw new Error(`Token could not be refreshed. ${err}`)
throw new Error(`Token could not be refreshed. ${err}`);
}
}
throw new Error(`Token could not be refreshed. No refresh token found`);
} else {
refreshTokenPromise = undefined
}
Expand Down

0 comments on commit 02acc70

Please sign in to comment.