Skip to content

Commit

Permalink
Workaround of #3194, when not in web worker. Now refreshing page does…
Browse files Browse the repository at this point in the history
… help when there was a token present but refresh token was not found.
  • Loading branch information
grzelaka authored and garrettjstevens committed Mar 11, 2023
1 parent ce05d01 commit 7f62be1
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 @@ -307,7 +307,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 @@ -331,9 +336,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 7f62be1

Please sign in to comment.