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'
|
2022-05-28 19:24:08 +02:00
|
|
|
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'
|
2022-05-28 19:24:08 +02:00
|
|
|
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
|
2021-02-05 13:45:02 +01:00
|
|
|
instance: Mastodon.Instance
|
2022-05-28 19:24:08 +02:00
|
|
|
appData: InstanceLatest['appData']
|
2021-01-07 19:13:09 +01:00
|
|
|
goBack?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
const InstanceAuth = React.memo(
|
2021-02-05 13:45:02 +01:00
|
|
|
({ 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
|
|
|
|
2021-11-15 22:34:43 +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-04-30 23:41:48 +02: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) {
|
|
|
|
await promptAsync()
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
}, [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,
|
2021-02-08 23:19:55 +01:00
|
|
|
instance,
|
2021-01-07 19:13:09 +01:00
|
|
|
appData
|
|
|
|
})
|
|
|
|
)
|
|
|
|
goBack && navigation.goBack()
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
}, [response])
|
|
|
|
|
|
|
|
return <></>
|
|
|
|
},
|
|
|
|
() => true
|
|
|
|
)
|
|
|
|
|
|
|
|
export default InstanceAuth
|