fix(actions/instances): Clear data when token revoked (#1446)

This commit is contained in:
Sorin Davidoi 2019-08-29 16:55:12 +02:00 committed by Nolan Lawson
parent 7d4cb1914a
commit 7d8f19a672

View file

@ -31,7 +31,7 @@ export function switchToInstance (instanceName) {
switchToTheme(instanceThemes[instanceName], enableGrayscale)
}
export async function logOutOfInstance (instanceName) {
export async function logOutOfInstance (instanceName, message = `Logged out of ${instanceName}`) {
const {
loggedInInstances,
instanceThemes,
@ -56,7 +56,7 @@ export async function logOutOfInstance (instanceName) {
composeData: composeData
})
store.save()
toast.say(`Logged out of ${instanceName}`)
toast.say(message)
const { enableGrayscale } = store.get()
switchToTheme(instanceThemes[newInstance], enableGrayscale)
/* no await */ database.clearDatabaseForInstance(instanceName)
@ -73,7 +73,7 @@ export async function updateVerifyCredentialsForInstance (instanceName) {
const { loggedInInstances } = store.get()
const accessToken = loggedInInstances[instanceName].access_token
await cacheFirstUpdateAfter(
() => getVerifyCredentials(instanceName, accessToken),
() => getVerifyCredentials(instanceName, accessToken).catch(logOutOnUnauthorized(instanceName)),
() => database.getInstanceVerifyCredentials(instanceName),
verifyCredentials => database.setInstanceVerifyCredentials(instanceName, verifyCredentials),
verifyCredentials => setStoreVerifyCredentials(instanceName, verifyCredentials)
@ -97,3 +97,13 @@ export async function updateInstanceInfo (instanceName) {
}
)
}
export function logOutOnUnauthorized (instanceName) {
return async error => {
if (error.message.startsWith('401:')) {
await logOutOfInstance(instanceName, `The access token was revoked, logged out of ${instanceName}`)
}
throw error
}
}