Twitter login in Ionic React Capacitor App
Tags: ionic, react, capacitor, twitter
Published: 2020-03-29 21:36:24

<main role="main"><article class=" u-minHeight100vhOffset65 u-overflowHidden postArticle postArticle--full is-supplementalPostContentLoaded is-withAccentColors" lang="en"><div class="postArticle-content js-postField js-notesSource editable" id="editor_6" g_editable="true" role="textbox" contenteditable="true" data-default-value="Title
Tell your story…" spellcheck="false"><section name="7088" class="section section--body section--first section--last"><div class="section-divider"><hr class="section-divider"></div><div class="section-content"><div class="section-inner sectionLayout--insetColumn"><p name="a5f6" class="graf graf--p graf-after--figure">In this post, you’ll learn how to implement <strong class="markup--strong markup--p-strong">Twitter login</strong> in your Capacitor apps, based on Ionic React framework. We will also retrieve user name and profile photo to show after login. And we’ll see how to auto-login users after first login.</p><p name="c222" class="graf graf--p graf-after--p">Since this post is a mix of three new frameworks, it is possible you landed here by mistake. If you are looking for Twitter login in</p><ul class="postList"><li name="f09a" class="graf graf--li graf-after--p">Ionic Angular (Cordova) apps&#8202;—&#8202;Please check <a href="https://enappd.com/blog/twitter-login-in-ionic-4-apps-using-firebase/24"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">this post</a></li><li name="334a" class="graf graf--li graf-after--li">React Native&#8202;—&#8202;Please check <a href="https://enappd.com/blog/twitter-login-in-react-native-apps-using-firebase/95"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">this post</a></li><li name="a6f7" class="graf graf--li graf-after--li">Ionic React Capacitor Apps&#8202;—&#8202;Continue reading 😎</li></ul><p name="fdd9" class="graf graf--p graf-after--li">As you can see from above, there are several options available for Hybrid app development these days, and it is easy to get confused between them. This post is focussed on <strong class="markup--strong markup--p-strong">Ionic framework</strong> with <a href="https://reactjs.org/"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank"><strong class="markup--strong markup--p-strong">React</strong></a> as the front-end framework, and <a href="https://capacitor.ionicframework.com/docs/"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank"><strong class="markup--strong markup--p-strong">Capacitor</strong></a> as runtime and build environment.</p><blockquote name="f425" class="graf graf--blockquote graf-after--p"><strong class="markup--strong markup--blockquote-strong"><em class="markup--em markup--blockquote-em">Code for this tutorial is available on Github repo </em></strong><a href="https://github.com/enappd/Ionic-Capacitor-Twitter-login"  class="markup--anchor markup--blockquote-anchor" rel="noopener"    target="_blank"><strong class="markup--strong markup--blockquote-strong"><em class="markup--em markup--blockquote-em">ionic-react-capacitor-twitter-login</em></strong></a></blockquote><p name="6ec3" class="graf graf--p graf-after--blockquote">Let’s see a brief intro to each of the included frameworks:</p><ol class="postList"><li name="430b" class="graf graf--li graf-after--p">Ionic</li><li name="0eb5" class="graf graf--li graf-after--li">Capacitor</li><li name="960b" class="graf graf--li graf-after--li">Ionic-React</li></ol><h3 name="5362" class="graf graf--h3 graf-after--li">What is Ionic&nbsp;?</h3><p name="f59a" class="graf graf--p graf-after--h3">In short&#8202;—&#8202;If you create Native apps in Android, you code in Java. If you create Native apps in iOS, you code in Obj-C or Swift. Both of these are powerful but complex languages. <strong class="markup--strong markup--p-strong">With Ionic and Cordova/Capacitor you can write a single piece of code for your app that can run on both iOS and Android </strong>(and windows!), that too with the simplicity of HTML, CSS, and JS.</p><p name="a73b" class="graf graf--p graf-after--p">It is important to note the contribution of Cordova/Capacitor in this. Ionic is only a UI wrapper made up of HTML, CSS and JS. So, by default, Ionic cannot run as an app in an iOS or Android device. Cordova/Capacitor is the build environment that containerizes (sort of) this Ionic web app and converts it into a device installable app, along with providing this app access to native APIs like Camera etc.</p><h3 name="f7d1" class="graf graf--h3 graf-after--p">Capacitor&#8202;—&#8202;How is it different from Cordova&nbsp;?</h3><blockquote name="59c5" class="graf graf--blockquote graf-after--h3"><strong class="markup--strong markup--blockquote-strong"><em class="markup--em markup--blockquote-em">Capacitor is very similar to Cordova, but with some key differences in the app workflow</em></strong></blockquote><p name="a887" class="graf graf--p graf-after--blockquote">Cordova helps build Ionic web app into a device installable app. But there are some limitations of Cordova, which Capacitor tries to overcome with a new App workflow.</p><p name="7f22" class="graf graf--p graf-after--p">Capacitor is a cross-platform app runtime that makes it easy to build web apps that run natively on iOS, Android, Electron, <em class="markup--em markup--p-em">and</em> the web. Ionic people call these apps “Native Progressive Web Apps” and they represent the next evolution beyond Hybrid apps.</p><p name="8e52" class="graf graf--p graf-after--p">Here are the differences between Cordova and Capacitor</p><ol class="postList"><li name="45e4" class="graf graf--li graf-after--p">Capacitor considers each platform project a <em class="markup--em markup--li-em">source asset</em> instead of a <em class="markup--em markup--li-em">build time asset</em>. That means, Capacitor wants you to keep the platform source code in the repository, unlike Cordova which always assumes that you will generate the platform code on build time</li><li name="9140" class="graf graf--li graf-after--li">Capacitor does not “run on device” or emulate through the command line. Instead, such operations occur through the platform-specific IDE. So you cannot run an Ionic-capacitor app using a command like <code class="markup--code markup--li-code">ionic run ios</code>&nbsp;. You will have to run iOS apps using Xcode, and Android apps using Android studio</li><li name="3c9d" class="graf graf--li graf-after--li">Because of the above, Capacitor does not use <code class="markup--code markup--li-code">config.xml</code> or a similar custom configuration for platform settings. Instead, configuration changes are made by editing <code class="markup--code markup--li-code">AndroidManifest.xml</code> for Android and <code class="markup--code markup--li-code">Info.plist</code> for Xcode</li><li name="05c2" class="graf graf--li graf-after--li">Since platform code is a <em class="markup--em markup--li-em">source asset, </em>you can directly change the native code using Xcode or Android Studio. This give more flexibility to developers. We will do some similar changes for Twitter login as well.</li></ol><h4 name="c44a" class="graf graf--h4 graf-after--li">Plugins</h4><p name="3a69" class="graf graf--p graf-after--h4">Cordova and <a href="https://ionicframework.com/docs/native"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">Ionic Native</a> plugins can be used in Capacitor environment. However, there are certain Cordova plugins which are known to be <a href="https://capacitor.ionicframework.com/docs/cordova/known-incompatible-plugins/"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">incompatible with Capacitor.</a> For Twitter login functionality, we’ll use the <a href="https://github.com/stewwan/capacitor-twitter"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">Capacitor Twitter login plugin</a></p><p name="2293" class="graf graf--p graf-after--p">Other than that, Capacitor also doesn’t support plugin installation with variables. Those changes have to be done manually in the native code. We will do something similar in this post.</p><h3 name="1566" class="graf graf--h3 graf-after--p">Why Ionic React&nbsp;?</h3><p name="90fd" class="graf graf--p graf-after--h3">Since Ionic 4, Ionic has become framework agnostic. Now you can create Ionic apps in Angular, React, Vue or even in plain JS. This gives Ionic great flexibility to be used by all kinds of developers.</p><p name="bdfd" class="graf graf--p graf-after--p"><strong class="markup--strong markup--p-strong">Ionic Angular apps are supported by both Cordova and Capacitor build environments.</strong></p><p name="d50b" class="graf graf--p graf-after--p">Same is not true for Ionic React apps&#8202;—&#8202;<strong class="markup--strong markup--p-strong">Ionic React apps are only supported by Capacitor build environment. </strong>Hence, if you want to build apps in Ionic React, you need to use Capacitor to build the app on device.</p><p name="4da2" class="graf graf--p graf-after--p">I know if can get confusing as three frameworks are crossing paths here. <strong class="markup--strong markup--p-strong">Bottom line for this post&#8202;—&#8202;Ionic + React + Capacitor + Capacitor Twitter Login plugin</strong></p><h3 name="a1e2" class="graf graf--h3 graf-after--p">A word on Twitter authentication</h3><p name="6fdd" class="graf graf--p graf-after--h3">We will use the <a href="https://github.com/stewwan/capacitor-twitter"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">Ionic Capacitor Twitter Plugin</a> to interact with Twitter and authenticate the user. Once the login is done, we receive user profile information.</p><p name="570a" class="graf graf--p graf-after--p">To receive user info, we will call the <code class="markup--code markup--p-code">/users/show.json</code> endpoint from Twitter (<a href="https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-show"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">more info here</a>). The important thing to note here is&#8202;—&#8202;Twitter does not support this API call from front-end (Client). Hence, we’ll have to call this end-point from a server. We will use Firebase Cloud Function to fetch this information. Firebase Cloud Function act as a REST API, and you can call them from your app.</p><blockquote name="f4e0" class="graf graf--blockquote graf-after--p">In case you don’t want to use Firebase for this, and you have another server at your disposal, feel free to use that for API call purpose.</blockquote><p name="ece8" class="graf graf--p graf-after--blockquote">This twitter plugin supports 3 functions</p><ul class="postList"><li name="186c" class="graf graf--li graf-after--p">Login</li><li name="61d2" class="graf graf--li graf-after--li">Logout</li><li name="1d1a" class="graf graf--li graf-after--li">isLogged&#8202;—&#8202;Checks if user is already logged in via Twitter.</li></ul><h3 name="319c" class="graf graf--h3 graf-after--li">Structure of&nbsp;post</h3><p name="1215" class="graf graf--p graf-after--h3">I always go step-by-step for readers of all experience level. If you know certain steps, feel free to skip them</p><p name="0242" class="graf graf--p graf-after--p"><strong class="markup--strong markup--p-strong">Step 1:</strong> Create a basic Ionic React app</p><p name="6cff" class="graf graf--p graf-after--p"><strong class="markup--strong markup--p-strong">Step 2:</strong> Connect Capacitor with your app</p><p name="afd3" class="graf graf--p graf-after--p"><strong class="markup--strong markup--p-strong">Step 3:</strong> Create a Twitter app in Developer console</p><p name="782d" class="graf graf--p graf-after--p"><strong class="markup--strong markup--p-strong">Step 4: </strong>Setup Twitter Login Plugin and functions</p><p name="7529" class="graf graf--p graf-after--p"><strong class="markup--strong markup--p-strong">Step 5</strong>: Fetch User profile from Twitter</p><p name="a7c6" class="graf graf--p graf-after--p"><strong class="markup--strong markup--p-strong">Step 6: </strong>Build and Test your app on Android</p><p name="dc6e" class="graf graf--p graf-after--p">Let’s get started with Ionic React Twitter login&nbsp;!</p><figure tabindex="0" contenteditable="false" name="85b2" class="graf graf--figure graf-after--p"><div class="aspectRatioPlaceholder is-locked" style="max-width: 500px; max-height: 281px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 56.2%;"></div><img class="graf-image" data-image-id="1*jAitkPgJ95kB9kXahTstQg.gif" data-width="500" data-height="281" src="https://cdn-images-1.medium.com/max/800/1*jAitkPgJ95kB9kXahTstQg.gif"><div class="crosshair u-ignoreBlock"></div></div><br/><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for image (optional)">Let’s get started&nbsp;!!!!</figcaption></figure><h3 name="a27e" class="graf graf--h3 graf-after--figure">Step 1&#8202;—&#8202;Create a basic Ionic-React app</h3><p name="9e7a" class="graf graf--p graf-after--h3">First you need to make sure you have the latest Ionic CLI. This will ensure you are using everything latest. Ensure latest Ionic CLI installation using</p><pre name="cf8a" class="graf graf--pre graf-after--p">$ <code class="markup--code markup--pre-code">npm install -g ionic@latest</code></pre><p name="f38b" class="graf graf--p graf-after--pre">Creating a basic Ionic-React app is not much different or difficult from creating a basic Ionic-Angular app. Start a basic <code class="markup--code markup--p-code">blank</code> starter using</p><pre name="d448" class="graf graf--pre graf-after--p">$ <code class="markup--code markup--pre-code">ionic start IonCapTwitter blank --type=react</code></pre><p name="39bc" class="graf graf--p graf-after--pre">The <code class="markup--code markup--p-code">--type=react</code> told the CLI to create a <strong class="markup--strong markup--p-strong">React</strong> app, not an <strong class="markup--strong markup--p-strong">Angular</strong> app&nbsp;!!</p><p name="8b9b" class="graf graf--p graf-after--p">Run the app in browser using (yes you guessed it right)</p><pre name="96ce" class="graf graf--pre graf-after--p">$ ionic serve</pre><p name="6306" class="graf graf--p graf-after--pre">You won’t see much in the homepage created in the starter. Let’s modify this page to include a button, icon and a title for login. Also, I have created a Homepage, where the user is redirected after successful login. The user profile information in this page comes after login and single API call.</p><figure tabindex="0" contenteditable="false" name="6e62" class="graf graf--figure graf-after--p"><div class="aspectRatioPlaceholder is-locked" style="max-width: 700px; max-height: 757px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 108.2%;"></div><img class="graf-image" data-image-id="1*qezuAdSLHE_4UiNLHxIXDA.png" data-width="2000" data-height="2164" alt="Login and Homepage for Ionic React Capacitor Twitter Login starter app" src="https://cdn-images-1.medium.com/max/800/1*qezuAdSLHE_4UiNLHxIXDA.png"><div class="crosshair u-ignoreBlock"></div></div><br/><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for image (optional)">Login and Homepage for Ionic React Capacitor Twitter Login starter&nbsp;app</figcaption></figure><p name="8472" class="graf graf--p graf-after--figure">The code for this can be found in the attached Github repository.</p><h3 name="35d2" class="graf graf--h3 graf-after--p">Step 2&#8202;—&#8202;Attach Capacitor to your Ionic-React app</h3><p name="4384" class="graf graf--p graf-after--h3">Capacitor can be attached to an existing Ionic app as well. To attach Capacitor to your existing Ionic app, run</p><pre name="e29e" class="graf graf--pre graf-after--p">$ <code class="markup--code markup--pre-code">ionic integrations enable capacitor</code></pre><p name="2503" class="graf graf--p graf-after--pre">This will attach Capacitor to your Ionic app. After this, you have to <code class="markup--code markup--p-code">init</code> the Capacitor app with</p><pre name="c8ac" class="graf graf--pre graf-after--p">$ npx cap init</pre><p name="c9af" class="graf graf--p graf-after--pre">It will ask you the app name and ID. Give app name whatever you want. App ID is the domain identifier of your app (ex: <code class="markup--code markup--p-code">com.example.app</code>). Note this ID as this will be required later when you create app in Twitter developer console.</p><h3 name="ee08" class="graf graf--h3 graf-after--p">Step 3&#8202;—&#8202;Create a Twitter app in Developer console</h3><p name="4950" class="graf graf--p graf-after--h3">To implement a twitter login, you will need a Twitter app (not THE Twitter app) i.e. you need to create an app in Twitter developer account.</p><ul class="postList"><li name="38f6" class="graf graf--li graf-after--p">Create a Twitter developer account&#8202;—&#8202;<a href="https://developer.twitter.com/"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">https://developer.twitter.com</a></li><li name="0cfa" class="graf graf--li graf-after--li">Create a Twitter app on <a href="https://developer.twitter.com/en/apps"  class="markup--anchor markup--li-anchor" rel="noopener" target="_blank">https://developer.twitter.com/en/apps</a></li><li name="b140" class="graf graf--li graf-after--li">When creating the app, make sure to&nbsp;<br>- Enable <strong class="markup--strong markup--li-strong">Twitter Sign-in</strong><br>- Put the <strong class="markup--strong markup--li-strong">Callback URL</strong> as <code class="markup--code markup--li-code">twittersdk://</code></li></ul><figure tabindex="0" contenteditable="false" name="bfd3" class="graf graf--figure graf-after--li"><div class="aspectRatioPlaceholder is-locked" style="max-width: 700px; max-height: 387px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 55.300000000000004%;"></div><img class="graf-image" data-image-id="1*LvbqRpcn7-5UFtT90gEN1A.png" data-width="1388" data-height="767" alt="Create a Twitter App with required options" src="https://cdn-images-1.medium.com/max/800/1*LvbqRpcn7-5UFtT90gEN1A.png"><div class="crosshair u-ignoreBlock"></div></div><br/><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for image (optional)">Create a Twitter App with required&nbsp;options</figcaption></figure><ul class="postList"><li name="a6ef" class="graf graf--li graf-after--figure">Once your app is created, find your Consumer API Key and Secret</li></ul><figure tabindex="0" contenteditable="false" name="3964" class="graf graf--figure graf-after--li"><div class="aspectRatioPlaceholder is-locked" style="max-width: 700px; max-height: 452px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 64.60000000000001%;"></div><img class="graf-image" data-image-id="1*OJRrIzkkz08RWHJYcFcVxw.png" data-width="955" data-height="617" alt="Twitter consumer API key and secret" src="https://cdn-images-1.medium.com/max/800/1*OJRrIzkkz08RWHJYcFcVxw.png"><div class="crosshair u-ignoreBlock"></div></div><br/><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for image (optional)">Twitter consumer API key and&nbsp;secret</figcaption></figure><h3 name="d697" class="graf graf--h3 graf-after--figure">Step 4&#8202;—&#8202;Setup Twitter Login Plugin and functions</h3><p name="8ec3" class="graf graf--p graf-after--h3">To setup Twitter login in the app, we’ll do following things</p><p name="c71f" class="graf graf--p graf-after--p">4.1 Install Capacitor Twitter Login plugin</p><p name="02c2" class="graf graf--p graf-after--p">4.2 Setup plugin functions for login</p><p name="aac8" class="graf graf--p graf-after--p">4.3 Enable routing between two pages of the app</p><p name="0828" class="graf graf--p graf-after--p">4.4 Control navigation via login / logout from Twitter</p><h4 name="18aa" class="graf graf--h4 graf-after--p">4.1 Install Capacitor Twitter Login&nbsp;plugin</h4><p name="5585" class="graf graf--p graf-after--h4">Install the plugin using</p><pre name="ef0f" class="graf graf--pre graf-after--p">$ npm install —-save capacitor-twitter</pre><p name="851f" class="graf graf--p graf-after--pre">Add the following info in your <code class="markup--code markup--p-code">capacitor.config.json</code></p><pre name="ccd7" class="graf graf--pre graf-after--p">...<br>"plugins": {<br> "TwitterPlugin": {<br> "consumerKey": "YOUR_API_KEY",<br> "consumerSecret":  "YOUR_API_SECRET"<br> }<br>}<br>...</pre><p name="8e9e" class="graf graf--p graf-after--pre">After this, build your app using following commands</p><pre name="b71a" class="graf graf--pre graf-after--p">// Build web assets<br>$ ionic build<br>// Add android platform<br>$ npx cap add android<br>// Copy all changes to Android platform<br>$ npx cap sync<br>// Open the project in Android studio<br>$ npx cap open android</pre><p name="ad06" class="graf graf--p graf-after--pre">In Android studio, locate file <code class="markup--code markup--p-code">android/app/src/main/java/**/**/MainActivity.java</code>, and add the plugin to the initialization list:</p><pre name="72a2" class="graf graf--pre graf-after--p">...<br><strong class="markup--strong markup--pre-strong">import io.stewan.capacitor.twitter.TwitterPlugin;<br>...<br></strong>public class MainActivity extends BridgeActivity {<br><strong class="markup--strong markup--pre-strong">...<br></strong>this.init(savedInstanceState, new ArrayList&lt;Class&lt;? extends Plugin&gt;&gt;() `{{<br>  ...<br>  <strong class="markup--strong markup--pre-strong">add (TwitterPlugin.class);</strong><br>  ...<br>}}`);</pre><figure tabindex="0" contenteditable="false" name="b101" class="graf graf--figure graf-after--pre"><div class="aspectRatioPlaceholder is-locked" style="max-width: 700px; max-height: 265px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 37.8%;"></div><img class="graf-image" data-image-id="1*gzvUK5GukmOngU39hdUP-Q.png" data-width="1042" data-height="394" alt="Import plugin in main.activity for Twitter login" src="https://cdn-images-1.medium.com/max/800/1*gzvUK5GukmOngU39hdUP-Q.png"><div class="crosshair u-ignoreBlock"></div></div><br/><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for image (optional)">Import plugin in main.activity for Twitter&nbsp;login</figcaption></figure><h4 name="9821" class="graf graf--h4 graf-after--figure">4.2 Setup plugin functions for&nbsp;login</h4><p name="7ac2" class="graf graf--p graf-after--h4">We have to setup four major functions for Twitter functionality</p><ol class="postList"><li name="3846" class="graf graf--li graf-after--p">Login</li><li name="ecac" class="graf graf--li graf-after--li">Logout</li><li name="f729" class="graf graf--li graf-after--li">Check if user is logged in (for auto-log in)</li><li name="4eca" class="graf graf--li graf-after--li">Get user profile information</li></ol><p name="cc8b" class="graf graf--p graf-after--li">Login page will have #1 and #3 functions, while the inner page (Home) will have #2 and #4.</p><h4 name="002d" class="graf graf--h4 graf-after--p">Login Page</h4><p name="8317" class="graf graf--p graf-after--h4">The complete code for Login Page looks like this</p><figure tabindex="0" contenteditable="false" name="65e6" class="graf graf--figure graf--iframe is-defaultValue graf-after--p"><div class="aspectRatioPlaceholder is-locked"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 35.699999999999996%;"></div><div class="iframeContainer">{% gist https://gist.github.com/enappd/6b543acc9bf394f03b0135e833f79082.js %}</div></div></figure><p name="16db" class="graf graf--p graf-after--figure">Some basic things</p><pre name="ba18" class="graf graf--pre graf-after--p">history.push({<br>   pathname: '/home',<br>   state: { userName: result.userName<br> }<br>});</pre><p name="a5ce" class="graf graf--p graf-after--pre">Here, <code class="markup--code markup--p-code">state</code> field contains navigation params (props). These will be accessed in next page from the <code class="markup--code markup--p-code">location</code> object of <code class="markup--code markup--p-code">props</code>&nbsp;.</p><p name="eb7c" class="graf graf--p graf-after--p"><code class="markup--code markup--p-code">getCurrentState</code> function gets the current user state from <code class="markup--code markup--p-code">isLogged</code> method, and you can use this to auto-login user after first time authentication. This way, you won’t ask user to authenticate each time he opens the app.</p><h4 name="8bcc" class="graf graf--h4 graf-after--p">Home Page</h4><p name="936d" class="graf graf--p graf-after--h4">The complete code for Home Page looks like this</p><figure tabindex="0" contenteditable="false" name="bac4" class="graf graf--figure graf--iframe is-defaultValue graf-after--p"><div class="aspectRatioPlaceholder is-locked"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 35.699999999999996%;"></div><div class="iframeContainer">{% gist https://gist.github.com/enappd/dced4892a6473fd97ee85a7d5b53fae8.js %}</div></div></figure><p name="f34a" class="graf graf--p graf-after--figure">The <code class="markup--code markup--p-code">userName</code> sent from first page is accessed via <code class="markup--code markup--p-code">this.props.location.state.userName</code>&nbsp;. This will be used further to fetch user’s profile information.</p><h4 name="4ade" class="graf graf--h4 graf-after--p">4.3 Enable routing between two pages of the&nbsp;app</h4><p name="c742" class="graf graf--p graf-after--h4">The routes are defined in <code class="markup--code markup--p-code">App.tsx</code> file like this</p><pre name="5f1c" class="graf graf--pre graf-after--p">&lt;IonApp&gt;<br> &lt;IonReactRouter&gt;<br>  &lt;IonRouterOutlet&gt;<br>   &lt;Route path="/login" component={Login} exact={true} /&gt;<br>   &lt;Route path="/home" component={Home} exact={true} /&gt;<br>   &lt;Route exact path="/" render={() =&gt; &lt;Redirect to="/login" /&gt;} /&gt;<br>  &lt;/IonRouterOutlet&gt;<br> &lt;/IonReactRouter&gt;<br>&lt;/IonApp&gt;</pre><h4 name="11e9" class="graf graf--h4 graf-after--pre">4.4 Control navigation via login / logout from&nbsp;Twitter</h4><p name="51dc" class="graf graf--p graf-after--h4">Once user is successfully logged in, we want to redirect to <code class="markup--code markup--p-code">Home</code> page. And when user logs out, we want to come back to <code class="markup--code markup--p-code">Login</code> page.</p><p name="a14b" class="graf graf--p graf-after--p">On <code class="markup--code markup--p-code">Login</code> page, <code class="markup--code markup--p-code">history.push</code> redirects user to next page after successful login</p><p name="1cea" class="graf graf--p graf-after--p">On <code class="markup--code markup--p-code">Login</code> page,<code class="markup--code markup--p-code">history.goBack()</code> takes you back to previous page after logout is successful.</p><h3 name="4e1e" class="graf graf--h3 graf-after--p">Step 5&#8202;—&#8202;Fetch user profile from&nbsp;Twitter</h3><p name="c016" class="graf graf--p graf-after--h3">Fetching user’s profile after logging in is a little tricky in Twitter. Since Twitter, does not provide support for client-side API calls for user profile, we will have to call the API through a server. The faster server we can make is a Firebase server, and use the Firebase Cloud Functions same as REST API.</p><blockquote name="fa8a" class="graf graf--blockquote graf-after--p">In case you don’t want to use Firebase for this, and you have another server at your disposal, feel free to use that for API call purpose. The API url and other things will remain the same</blockquote><h4 name="febb" class="graf graf--h4 graf-after--blockquote">Initial Setup</h4><p name="5587" class="graf graf--p graf-after--h4">To create Firebase Cloud Function, you can follow <a href="https://enappd.com/blog/firebase-cloud-functions-in-ionic-4-complete-guide/32"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">this detailed blog</a>. Basic steps are</p><ul class="postList"><li name="5a01" class="graf graf--li graf-after--p">Create a Firebase project</li><li name="471c" class="graf graf--li graf-after--li">Install firebase CLI using <code class="markup--code markup--li-code">npm i firebase-tools -g</code></li><li name="06c0" class="graf graf--li graf-after--li">Initialize a Firebase project using <code class="markup--code markup--li-code">firebase init</code>&nbsp;. Select the project from the list, and select <code class="markup--code markup--li-code">functions</code> from the list of options</li><li name="75e0" class="graf graf--li graf-after--li">Install packages. This will create a <code class="markup--code markup--li-code">functions</code> folder in your root. Inside this, the <code class="markup--code markup--li-code">index.js</code> file will contain the actual functions</li></ul><h4 name="c28b" class="graf graf--h4 graf-after--li">Get Twitter App Bearer&nbsp;Token</h4><p name="e318" class="graf graf--p graf-after--h4">To fetch user’s info after login, you will need a <code class="markup--code markup--p-code">bearer-token</code> to send along with the API request. This <code class="markup--code markup--p-code">bearer-token</code> can be created using a <code class="markup--code markup--p-code">CURL</code> command like following</p><pre name="6f62" class="graf graf--pre graf-after--p">curl -u 'API key:API secret key' \   --data 'grant_type=client_credentials' \   'https://api.twitter.com/oauth2/token'</pre><p name="ad9a" class="graf graf--p graf-after--pre">where <code class="markup--code markup--p-code">API key</code> is your Twitter API Key, and <code class="markup--code markup--p-code">API secret key</code> is your Twitter App secret. You will receive a response like this, which contains <code class="markup--code markup--p-code">bearer-token</code></p><pre name="a88d" class="graf graf--pre graf-after--p">{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAMLheAAAAAAA0%2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXMDjRfXXXXXb0wnz4XsNiRVBChTYbJcE3F"}</pre><h4 name="4011" class="graf graf--h4 graf-after--pre">Firebase function to fetch profile&nbsp;info</h4><p name="593c" class="graf graf--p graf-after--h4">We’ll create a function <code class="markup--code markup--p-code">getUserInfo</code> which will fetch user’s info in the Firebase Function. We will send <code class="markup--code markup--p-code">userName</code>, which we received from login, in the request. In response we will get all the profile details. The function is as follows</p><figure tabindex="0" contenteditable="false" name="320e" class="graf graf--figure graf--iframe is-defaultValue graf-after--p"><div class="aspectRatioPlaceholder is-locked"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 35.699999999999996%;"></div><div class="iframeContainer">{% gist https://gist.github.com/enappd/19358951937ac7a286280b900483a692.js %}</div></div></figure><h4 name="96ff" class="graf graf--h4 graf-after--figure">Deploy function on Firebase for&nbsp;use</h4><p name="6820" class="graf graf--p graf-after--h4">You will need to deploy the function on Firebase to use it as REST API call. To deploy the function, just run</p><pre name="9fe3" class="graf graf--pre graf-after--p">$ firebase deploy</pre><p name="3516" class="graf graf--p graf-after--pre">This will deploy the function on your Firebase project. You can access the function using url like <code class="markup--code markup--p-code"><a href="https://us-central1-auth-demos.cloudfunctions.net/getUserInfo"  class="markup--anchor markup--p-anchor" rel="nofollow noopener" target="_blank">https://us-central1-auth-demos.cloudfunctions.net/getUserInfo</a></code>&nbsp;. This URL will be shown in your terminal when you deploy the functions.</p><h3 name="7b29" class="graf graf--h3 graf-after--p">Step 6— Build and Test your app on&nbsp;Android</h3><p name="b78e" class="graf graf--p graf-after--h3">At this point, you have all the Twitter Login stuff built-in, Android app setup and Twitter app also setup. Build the app once again after all changes using</p><pre name="94e5" class="graf graf--pre graf-after--p">// Build web assets<br>$ <strong class="markup--strong markup--pre-strong">ionic build<br></strong>// Copy all changes to Android platform<br>$ <strong class="markup--strong markup--pre-strong">npx cap sync</strong></pre><p name="f517" class="graf graf--p graf-after--pre">Build the app on Android device using Android studio. You should be able to login via Twitter, and reach the inner page where you see the user information. Following GIF shows the login flow in my OnePlus device.</p><figure tabindex="0" contenteditable="false" name="45a9" class="graf graf--figure graf-after--p"><div class="aspectRatioPlaceholder is-locked" style="max-width: 350px; max-height: 622px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 177.7%;"></div><img class="graf-image" data-image-id="1*WbtAc6bUXr9WLVPWKdAIGA.gif" data-width="350" data-height="622" src="https://cdn-images-1.medium.com/max/800/1*WbtAc6bUXr9WLVPWKdAIGA.gif"><div class="crosshair u-ignoreBlock"></div></div><br/><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for image (optional)">Twitter login in Ionic React Capacitor App</figcaption></figure><p name="d8bc" class="graf graf--p graf-after--figure">The login success response looks like following</p><figure tabindex="0" contenteditable="false" name="b050" class="graf graf--figure is-defaultValue graf-after--p"><div class="aspectRatioPlaceholder is-locked" style="max-width: 573px; max-height: 142px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 24.8%;"></div><img class="graf-image" data-image-id="1*u_Q3QsUaJghCylHsyYqEqw.png" data-width="573" data-height="142" src="https://cdn-images-1.medium.com/max/800/1*u_Q3QsUaJghCylHsyYqEqw.png"><div class="crosshair u-ignoreBlock"></div></div></figure><p name="33be" class="graf graf--p graf-after--figure">When you call the Firebase function to fetch user’s profile information, the response looks like this</p><figure tabindex="0" contenteditable="false" name="031d" class="graf graf--figure graf--iframe graf-after--p"><div class="aspectRatioPlaceholder is-locked"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 35.699999999999996%;"></div><div class="iframeContainer">{% gist https://gist.github.com/enappd/051be174445ee5fc8f12c0c26bd3030a.js %}</div></div><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for embed (optional)">Response from Twitter user profile information fetch</figcaption></figure><p name="e3b1" class="graf graf--p graf-after--figure">This is a lot of info, and you can make use of it as you want.</p><p name="c6fc" class="graf graf--p graf-after--p is-selected"><strong class="markup--strong markup--p-strong">Note</strong>: To make a successful API call from Firebase function, your Firebase project needs to be on paid plan (Choose Blaze Plan). Don’t worry, it does not deduct any money immediately. Several thousand requests are free, you just need to add a payment method. You can check details for pricing <a href="https://firebase.google.com/pricing"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">here</a>.</p><blockquote name="52b1" class="graf graf--blockquote graf-after--p">In case you don’t want to use Firebase for this, and you have another server at your disposal, feel free to use that for API call purpose.</blockquote><h3 name="49f9" class="graf graf--h3 graf-after--blockquote">Conclusion</h3><p name="40e8" class="graf graf--p graf-after--h3">In this post you learnt how to implement Twitter login in your Ionic React Capacitor app. Social logins are very important part of your apps, as they make users trust your apps more. It is also easy to use, and users don’t have to remember any passwords. You can always link the Social logins with your server as well.</p><p name="0658" class="graf graf--p graf-after--p">Firebase has a ready-to-integrate solution for this. You can integrate your Twitter login with Firebase to enable Firebase to handle the auth token etc.</p><blockquote name="9ed2" class="graf graf--blockquote graf-after--p"><strong class="markup--strong markup--blockquote-strong"><em class="markup--em markup--blockquote-em">Code for this tutorial is available on Github repo </em></strong><a href="https://github.com/enappd/Ionic-Capacitor-Twitter-login"  class="markup--anchor markup--blockquote-anchor" rel="noopener"    target="_blank"><strong class="markup--strong markup--blockquote-strong"><em class="markup--em markup--blockquote-em">ionic-react-capacitor-twitter-login</em></strong></a></blockquote><h3 name="8963" class="graf graf--h3 graf-after--blockquote">Next Steps</h3><p name="3e7f" class="graf graf--p graf-after--h3">Now that you have learned the implementation of Twitter Login in Ionic React Capacitor app, you can also try following blogs for other <strong class="markup--strong markup--p-strong">Ionic apps</strong></p><h4 name="35e7" class="graf graf--h4 graf-after--p">Ionic React Capacitor</h4><ul class="postList"><li name="5a6e" class="graf graf--li graf-after--h4"><a href="https://enappd.com/blog/facebook-login-in-ionic-react-capacitor-apps/118"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Facebook Login in Ionic React Capacitor Apps</a></li><li name="8885" class="graf graf--li graf-after--li"><a href="https://enappd.com/blog/how-to-make-ionic-apps-in-react-using-capacitor/26"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">How to make basic apps in ionic-react</a>-capacitor</li><li name="6594" class="graf graf--li graf-after--li"><a href="https://enappd.com/blog/camera-and-gallery-in-ionic-react-app-using-capacitor/110"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Camera and Image gallery in Ionic-React-Capacitor</a></li><li name="4f4f" class="graf graf--li graf-after--li"><a href="https://enappd.com/blog/firebase-push-notification-in-ionic-react-capacitor/111"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Push notification in Ionic-React-Capacitor apps</a></li><li name="c5a7" class="graf graf--li graf-after--li"><a href="https://enappd.com/blog/play-music-in-ionic-capacitor-apps/112"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Playing Music in Ionic Capacitor apps</a></li><li name="66fd" class="graf graf--li graf-after--li"><a href="https://enappd.com/blog/icon-splash-in-ionic-react-capacitor-apps/114"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Adding Icon and Splash in Ionic React Capacitor apps</a></li><li name="d39b" class="graf graf--li graf-after--li"><a href="https://enappd.com/blog/html5-games-in-ionic-capacitor-using-phaser/115"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Create HTML5 games in Ionic Capacitor apps using Phaser</a></li></ul><p name="9ed9" class="graf graf--p graf-after--li">If you need a base to start your next <strong class="markup--strong markup--p-strong">Ionic 4 React app</strong>, you can make your next awesome app using <a href="https://store.enappd.com/product/ionic-react-full-app-capacitor/"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">Ionic React Full App</a></p><figure tabindex="0" contenteditable="false" name="074b" class="graf graf--figure graf-after--p"><div class="aspectRatioPlaceholder is-locked" style="max-width: 600px; max-height: 600px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 100%;"></div><img class="graf-image" data-image-id="0*AxzmDBUVguZ1kw_i.jpeg" data-width="600" data-height="600" src="https://cdn-images-1.medium.com/max/800/0*AxzmDBUVguZ1kw_i.jpeg"><div class="crosshair u-ignoreBlock"></div></div><br/><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for image (optional)">Ionic 4 React Full App with huge number of layouts and&nbsp;features</figcaption></figure><h4 name="9d27" class="graf graf--h4 graf-after--figure">Ionic Angular</h4><ul class="postList"><li name="85a8" class="graf graf--li graf-after--h4">Ionic 4 Payment Gateways&#8202;—&#8202;<a href="https://enappd.com/blog/ionic-4-stripe-payment-integration-with-firebase-for-apps-and-pwa/17"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Stripe</a> | <a href="https://enappd.com/blog/ionic-4-paypal-payment-integration-for-apps-and-pwa/16"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">PayPal</a> | <a href="https://enappd.com/blog/how-to-integrate-apple-pay-in-ionic-4-apps/21"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Apple Pay</a> | <a href="https://enappd.com/blog/how-to-integrate-razorpay-in-ionic-4-apps-and-pwa/20"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">RazorPay</a></li><li name="5acf" class="graf graf--li graf-after--li">Ionic 4 Charts with&#8202;—&#8202;<a href="https://enappd.com/blog/ionic-4-charts-using-google-charts/66"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Google Charts </a>| <a href="https://enappd.com/blog/adding-charts-in-ionic-4-apps-and-pwa-part-3/60"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">HighCharts</a> | <a href="https://enappd.com/blog/adding-charts-in-ionic-4-apps-and-pwa-part-2/54"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">d3.js</a> | <a href="https://enappd.com/blog/charts-in-ionic-4-apps-and-pwa-part-1/52"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Chart.js</a></li><li name="dfc7" class="graf graf--li graf-after--li">Ionic 4 Social Logins&#8202;—&#8202;<a href="https://enappd.com/blog/facebook-login-in-ionic-4-apps-using-firebase/25"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Facebook</a> | <a href="https://enappd.com/blog/google-login-in-ionic-4-apps-using-firebase/39"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Google</a> | <a href="https://enappd.com/blog/twitter-login-in-ionic-4-apps-using-firebase/24"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Twitter</a></li><li name="6667" class="graf graf--li graf-after--li">Ionic 4 Authentications&#8202;—&#8202;<a href="https://enappd.com/blog/email-authentication-with-firebase-in-ionic-4/38"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Via Email</a> | <a href="https://enappd.com/blog/firebase-anonymous-login-in-ionic-4/37"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Anonymous</a></li><li name="91ad" class="graf graf--li graf-after--li">Ionic 4 Features&#8202;—&#8202;<a href="https://enappd.com/blog/using-geolocation-geocoding-and-reverse-geocoding-in-ionic-4/45"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Geolocation</a> | <a href="https://enappd.com/blog/qr-code-scanning-and-optical-character-recognition-ocr-in-ionic-4/9"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">QR Code reader </a>| <a href="https://enappd.com/blog/best-fitness-plugins-for-ionic-4-how-to-use-pedometer/15"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Pedometer</a></li><li name="a628" class="graf graf--li graf-after--li">Media in Ionic 4&#8202;—&#8202;<a href="https://enappd.com/blog/spotify-like-music-in-ionic-4-apps/48"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Audio</a> | <a href="https://enappd.com/blog/adding-video-player-in-ionic-4-app/64"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Video</a> | <a href="https://enappd.com/blog/image-picker-in-ionic-4-app/67"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Image Picker</a> | <a href="https://enappd.com/blog/how-to-add-image-cropper-in-ionic-4-app/68"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Image Cropper</a></li><li name="779c" class="graf graf--li graf-after--li">Ionic 4 Essentials&#8202;—&#8202;<a href="https://enappd.com/blog/ionic-4-save-and-retrieve-data-locally-on-device/59"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Native Storage</a> | <a href="https://enappd.com/blog/how-to-translate-in-ionic-4-globalization-internationalization-and-localization/11"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Translations</a> | <a href="https://enappd.com/blog/rtl-right-to-left-use-in-ionic-4/50"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">RTL</a></li><li name="38ee" class="graf graf--li graf-after--li">Ionic 4 messaging&#8202;—&#8202;<a href="https://enappd.com/blog/implement-ionic-4-firebase-push/34"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Firebase Push</a> | <a href="https://enappd.com/blog/automatically-read-sms-in-ionic-4-apps/42"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Reading SMS</a></li><li name="0711" class="graf graf--li graf-after--li">Ionic 4 with Firebase&#8202;—&#8202;<a href="https://enappd.com/blog/how-to-integrate-firebase-in-ionic-4-apps/23"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Basics</a> | <a href="https://enappd.com/blog/firebase-with-ionic-4-hosting-auth-and-db-connection/58"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Hosting and DB</a> | <a href="https://enappd.com/blog/firebase-cloud-functions-in-ionic-4-complete-guide/32"  class="markup--anchor markup--li-anchor" rel="noopener"    target="_blank">Cloud functions</a></li></ul><p name="12ae" class="graf graf--p graf-after--li">If you need a base to start your next <strong class="markup--strong markup--p-strong">Ionic 4 Angular app</strong>, you can make your next awesome app using <a href="https://store.enappd.com/product/ionic-4-full-app"  class="markup--anchor markup--p-anchor" rel="noopener"    target="_blank">Ionic 4 Full App</a></p><figure tabindex="0" contenteditable="false" name="001a" class="graf graf--figure graf-after--p"><div class="aspectRatioPlaceholder is-locked" style="max-width: 560px; max-height: 353px;"><div class="aspectRatioPlaceholder-fill" style="padding-bottom: 63%;"></div><img class="graf-image" data-image-id="0*lqQcM0JyxN7nFQdL.jpeg" data-width="560" data-height="353" src="https://cdn-images-1.medium.com/max/800/0*lqQcM0JyxN7nFQdL.jpeg"><div class="crosshair u-ignoreBlock"></div></div><br/><figcaption class="imageCaption" contenteditable="true" data-default-value="Type caption for image (optional)">Ionic 4 Full App with huge number of layouts and&nbsp;features</figcaption></figure><p name="a694" class="graf graf--p graf--empty graf-after--figure graf--trailing"><br></p></div></div></section></div></article></main>