tooot/src/utils/slices/instances/remove.ts

39 lines
1002 B
TypeScript
Raw Normal View History

2021-02-20 19:12:44 +01:00
import { createAsyncThunk } from '@reduxjs/toolkit'
import * as AuthSession from 'expo-auth-session'
2021-03-01 00:28:14 +01:00
import { Instance } from '../instancesSlice'
import { updateInstancePush } from './updatePush'
2021-02-20 19:12:44 +01:00
const removeInstance = createAsyncThunk(
'instances/remove',
2021-03-01 00:28:14 +01:00
async (instance: Instance, { dispatch }): Promise<Instance> => {
if (instance.push.global.value) {
dispatch(updateInstancePush(false))
}
2021-02-20 19:12:44 +01:00
2021-03-01 00:28:14 +01:00
let revoked = undefined
try {
revoked = await AuthSession.revokeAsync(
{
clientId: instance.appData.clientId,
clientSecret: instance.appData.clientSecret,
token: instance.token,
scopes: ['read', 'write', 'follow', 'push']
},
{
revocationEndpoint: `https://${instance.url}/oauth/revoke`
}
)
} catch {
console.warn('Revoking error')
}
2021-02-20 19:12:44 +01:00
2021-03-01 00:28:14 +01:00
if (!revoked) {
console.warn('Revoking error')
2021-02-20 19:12:44 +01:00
}
2021-03-01 00:28:14 +01:00
return Promise.resolve(instance)
2021-02-20 19:12:44 +01:00
}
)
export default removeInstance