tooot/src/components/Instance/Auth.tsx

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-12-04 00:35:13 +01:00
import browserPackage from '@helpers/browserPackage'
2021-01-07 19:13:09 +01:00
import { useNavigation } from '@react-navigation/native'
2022-04-30 23:47:52 +02:00
import { useAppDispatch } from '@root/store'
import { InstanceLatest } from '@utils/migrations/instances/migration'
2021-08-29 16:08:02 +02:00
import { TabMeStackNavigationProp } from '@utils/navigation/navigators'
2021-02-20 19:12:44 +01:00
import addInstance from '@utils/slices/instances/add'
import { checkInstanceFeature } from '@utils/slices/instancesSlice'
2021-01-07 19:13:09 +01:00
import * as AuthSession from 'expo-auth-session'
import React, { useEffect } from 'react'
import { useQueryClient } from 'react-query'
2022-04-30 23:47:52 +02:00
import { useSelector } from 'react-redux'
2021-01-07 19:13:09 +01:00
export interface Props {
instanceDomain: string
2021-01-17 22:37:05 +01:00
// Domain can be different than uri
instance: Mastodon.Instance
appData: InstanceLatest['appData']
2021-01-07 19:13:09 +01:00
goBack?: boolean
}
const InstanceAuth = React.memo(
({ instanceDomain, instance, appData, goBack }: Props) => {
2021-01-28 00:41:53 +01:00
const redirectUri = AuthSession.makeRedirectUri({
native: 'tooot://instance-auth',
useProxy: false
})
2021-01-27 00:35:34 +01:00
2022-11-29 21:59:52 +01:00
const navigation = useNavigation<TabMeStackNavigationProp<'Tab-Me-Root' | 'Tab-Me-Switch'>>()
2021-01-07 19:13:09 +01:00
const queryClient = useQueryClient()
2022-04-30 23:47:52 +02:00
const dispatch = useAppDispatch()
2021-01-07 19:13:09 +01:00
2022-11-29 21:59:52 +01:00
const deprecateAuthFollow = useSelector(checkInstanceFeature('deprecate_auth_follow'))
2021-01-07 19:13:09 +01:00
const [request, response, promptAsync] = AuthSession.useAuthRequest(
{
clientId: appData.clientId,
clientSecret: appData.clientSecret,
2022-04-30 23:41:48 +02:00
scopes: deprecateAuthFollow
? ['read', 'write', 'push']
: ['read', 'write', 'follow', 'push'],
2021-01-16 14:16:58 +01:00
redirectUri
2021-01-07 19:13:09 +01:00
},
{
authorizationEndpoint: `https://${instanceDomain}/oauth/authorize`
}
)
useEffect(() => {
;(async () => {
if (request?.clientId) {
2022-12-04 00:35:13 +01:00
await promptAsync({ browserPackage: await browserPackage() }).catch(e => console.log(e))
2021-01-07 19:13:09 +01:00
}
})()
}, [request])
useEffect(() => {
;(async () => {
if (response?.type === 'success') {
const { accessToken } = await AuthSession.exchangeCodeAsync(
{
clientId: appData.clientId,
clientSecret: appData.clientSecret,
scopes: ['read', 'write', 'follow', 'push'],
2021-01-16 14:16:58 +01:00
redirectUri,
2021-01-07 19:13:09 +01:00
code: response.params.code,
extraParams: {
grant_type: 'authorization_code'
}
},
{
tokenEndpoint: `https://${instanceDomain}/oauth/token`
}
)
queryClient.clear()
dispatch(
2021-02-20 19:12:44 +01:00
addInstance({
domain: instanceDomain,
2021-01-07 19:13:09 +01:00
token: accessToken,
instance,
2021-01-07 19:13:09 +01:00
appData
})
)
goBack && navigation.goBack()
}
})()
}, [response])
return <></>
},
() => true
)
export default InstanceAuth