Technetium 101(React Native): Twitter Social Login

 


Version

  • react-native: 0.63.4
  • react-native-login-twitter: ^3.0.4

Step 1 : Go to Twitter Apps to create your app so that you can obtain API key and secret.

  • Only if selected as professional, you will be needed tp fill in your team profile. Refer image below.

  • Then you need to write how you plan to use Twitter data/Twitter API. Minimum of 200 characters.

  • After that read the developer agreement & policy and submit application. 
  • It may take up to 72 hours to verify your developer's account.




  • After your developer account been verified, you can now create new app
  • By default, Twitter won't allow you to grab user's email, so you have to apply for a permission for your app to retrieve user's email
  • Go to your projects & Apps and select your app
  • Then click Authentication settings
  • Need to enable 3-logged OAuth and request email address
  • Remember to set a Callback Url, whatever will work


Step 2 : Add react-native-login-twitter library

Add library
  • yarn add react-native-login-twitter

Additional step for IOS
  • ..

Step 3 : Configuration ANDROID/IOS Projects

IOS

..

Android

Autolink for react native version 0.60.0+

Step 4 : Add code in react native project for Login

import React, { Component } from "react"
import {
  AppRegistry,
  Button,
  StyleSheet,
  Text,
  View,
  Alert,
  NativeModules,
  TouchableOpacity } from "react-native"
// import { RNTwitterSignIn } from 'react-native-login-twitter';
const { RNTwitterSignIn } = NativeModules

const Constants = {
  //Dev Parse keys
  TWITTER_COMSUMER_KEY: "Your_consumer_key",
  TWITTER_CONSUMER_SECRET: "Your_consumer_secret_key"
}

export default class LoginScreen5 extends Component {
  state = {
    isLoggedIn: false,
    userInfo:null,
  }

  componentDidMount(){
    // alert(Constants.TWITTER_COMSUMER_KEY)
  }

  _twitterSignIn = () => {
    RNTwitterSignIn.init(Constants.TWITTER_COMSUMER_KEY, Constants.TWITTER_CONSUMER_SECRET)
    RNTwitterSignIn.logIn()
      .then(loginData => {
        console.log(loginData)
        const { authToken, authTokenSecret } = loginData
        if (authToken && authTokenSecret) {
          this.setState({
            isLoggedIn: true,
            userInfo:loginData,
          })
        }
      })
      .catch(error => {
        console.log(error)
      }
    )
  }

  handleLogout = () => {
    console.log("logout")
    RNTwitterSignIn.logOut()
    this.setState({
      isLoggedIn: false
    })
  }

  render() {
    const { isLoggedIn } = this.state
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        {isLoggedIn
          ? 
            <View>
              <Text>Email: {this.state.userInfo.email}</Text>
              <Text>Name: {this.state.userInfo.name}</Text>
              <Text>Username: {this.state.userInfo.userName}</Text>
              <Button name="logo-twitter" style={styles.button} onPress={this.handleLogout} title="Logout">
              </Button>
            </View>
          : <Button name="logo-twitter" style={styles.button} onPress={this._twitterSignIn} title="Login with Twitter">
            </Button>}
      </View>
    )
  }
}

const styles = StyleSheet.create({
  button: {
    backgroundColor: '#1b95e0',
    color: 'white',
    width: 200,
    height: 50
  }
})


Reference

Package



Comments

Popular posts from this blog

Build App Insights: Mobile app development trends 2021

Build App Insights: Skeleton Screens Benefits

Technetium 101(React Native): Performance Optimization