React Native Login With X

date
Oct 3, 2024
slug
react-native-connect-with-x
status
Published
tags
Front-end
summary
WebBrowser.maybeCompleteAuthSession();
type
Post
import { useEffect } from 'react'; import * as WebBrowser from 'expo-web-browser'; import { makeRedirectUri, useAuthRequest } from 'expo-auth-session'; import { Button, Platform } from 'react-native'; WebBrowser.maybeCompleteAuthSession(); // Endpoint const discovery = { authorizationEndpoint: "https://twitter.com/i/oauth2/authorize", tokenEndpoint: "https://twitter.com/i/oauth2/token", revocationEndpoint: "https://twitter.com/i/oauth2/revoke", }; export default function App() { const [request, response, promptAsync] = useAuthRequest( { clientId: 'CLIENT_ID', redirectUri: makeRedirectUri({ scheme: 'your.app', }), usePKCE: true, scopes: [ "tweet.read", ], }, discovery ); useEffect(() => { if (response?.type === 'success') { const { code } = response.params; } }, [response]); return ( <Button disabled={!request} title="Login" onPress={() => { promptAsync(); }} /> ); }
  • You will need to be approved by Twitter support before you can use the Twitter v2 API.
  • Web does not appear to work, the Twitter authentication website appears to block the popup, causing the response of useAuthRequest to always be {type: 'dismiss'}.
  • Example redirects:
    • Standalone / development build: com.your.app://
    • Web (dev expo start --https): https://localhost:19006 (no ending slash)
  • The redirectUri requires two slashes (://).
 
Cấu hình X:
notion image