Twitter Login in Ionic Framework Apps
In this ionic tutorial we are going to show you - step by step - how to integrate Twitter Login to your Ionic framework apps. This will enable an easy and elegant way for your users to log in to your apps.
Why adding Twitter Authentication into your ionic framework app will benefit you and your users?
- Improve conversions: Help people log into your app quickly without having to remember another username and password.
- One login across every device: Make it easy for people to log in and access their info across multiple platforms and devices.
- Build a trusted relationship: Give people control over the info they share with your app.
- Access profile info: like picture, gender, age, name, without having to ask for it.
Twitter Authentication in Ionic Framework
There are different ways to integrate Twitter authentication into your Ionic Framework App. However, only the way covered in this post uses the native approach which uses Twitter app to perform the login instead of opening a popup requesting users to enter their credentials to login to Twitter before granting access to your app.
The way it works for hybrid apps to use native APIS and SDKS is simple, you need a piece (typically a Cordova plugin) that connects native APIS and SDKS with the javascript environment where the hybrid apps run. In this case we are going to use a cordova plugin to interact with the Twitter API.
We will use Twitter Connect Plugin to login with Twitter on iOS and Android. You will not only get the name of the user, but also stuff like their full name, followers, and picture.
Ionic Twitter Authentication
For this Ionic example app you will need:
- An Ionic app where you will integrate this login. You can either use a blank app, or an existing one.
- Fabric API key.
- Register a Twitter App and get the Twitter Consumer Key and Twitter Consumer Secret
-
The
Twitter Connect Plugin
to interact with the native Twitter App.
Don't worry, in this twitter authentication with ionic tutorial we will guide you through these steps.
Our Ionic demo app will look like this:



Get a Twitter Fabric API key
Using Twitter's Fabric SDK, you can enable Single Sign On with your Android and iOS apps. To use Fabric, you will need to create an account.
What is Fabric?
Twitter launched Fabric as a modular SDK in 2014 to allow developers to pick and choose different tools to improve their apps. In 2017 Google acquired Fabric with the plan to integrate it with Firebase. Apparently, today you still need to create a Fabric account in order to install the Twitter Connect Plugin because the FABRIC_API_KEY is a required parameter.

After creating the account just go to this link and log in with your new Fabric account. You don't need to do the onboarding process. The only thing we really need from Fabric is the API key, so try not to get messed with other options.
Scroll down to Add Your API Key section and you will see the following code with your app API KEY.
Create your Twitter app
In this section I will help you creating your Twitter app in order to get the consumer key and consumer secret.- Go to your Twitter Application Management
- To create an app, you need a Twitter developer account. If you don't have you can create one.
- Follow the steps to Create a new app or use an existing one.
-
When creating the app make sure to set
Callback URL=twittersdk://
and to enableSign in with Twitter
.
Install Twitter Connect Plugin
After you have your Fabric API key, and your Twitter App Consumer API keys, it’s time to install the cordova plugin into your Ionic app.
Execute the following command on the terminal changing the variables Fabric API Key
, Twitter Consumer Key
and Twitter Consumer Secret
with your own values. (Also remove the <>)
npm install --save @ionic-native/twitter-connect
What we have done so far in this Ionic tutorial:
- Created an Ionic app (or used a existing one)
- Created a Fabric Account
- Created a Twitter app (or used a existing one)
- Installed Twitter Connect Plugin into your Ionic framework app
Add Twitter Log In and Log Out
Now we will go straight to the code so open your Ionic project with your preferred code editor. Personally we use atom.
We don't want to ask users to log in each time they open the app because it would be annoying. To solve this we will use NativeStorage to save the user data the first time they log in.
NativeStorage
NativeStorage is a Cordova plugin which provides persistent storage of primitives native in Android, iOS and Windows. It's created because of the non-persistent property of LocalStorage in the WebView of Android and iOS. In iOS stored data from LocalStorage can be removed by the OS, when running out of memory.
To install native storage plugin just run the following commands in your ionic project:
$ ionic cordova plugin add cordova-plugin-nativestorage --save $ npm install --save @ionic-native/native-storage
To know if the user is already logged in, we will add the following code to check this in the platform.ready()
in app.component.ts
.
export class AppComponent { constructor( private platform: Platform, private splashScreen: SplashScreen, private statusBar: StatusBar, private nativeStorage: NativeStorage, private router: Router ) { this.initializeApp(); } initializeApp() { this.platform.ready().then(() => { //Here we will check if the user is already logged in //because we don't want to ask users to log in each time they open the app this.nativeStorage.getItem('twitter_user') .then(data => { //user is previously logged and we have his data //we will let him access the app this.router.navigate(["/user"]); this.splashScreen.hide(); }, err => { this.router.navigate(["/login"]); this.splashScreen.hide(); }) this.statusBar.styleDefault(); }); } }
Ionic Twitter Log In
The best way to show you how to add log in functionality to your Ionic Framework App is with a real example of the code, here you can see some Typescript code that handles the all the twitter authentication for your ionic app.
import { Component } from '@angular/core'; import { TwitterConnect } from '@ionic-native/twitter-connect/ngx'; import { Router } from '@angular/router'; import { NativeStorage } from '@ionic-native/native-storage/ngx'; import { LoadingController } from '@ionic/angular'; @Component({ selector: 'app-login', templateUrl: './login.page.html', styleUrls: ['./login.page.scss'], }) export class LoginPage { constructor( private tw: TwitterConnect, private nativeStorage: NativeStorage, public loadingController: LoadingController, private router: Router ) { } async doTwLogin(){ const loading = await this.loadingController.create({ message: 'Please wait...' }); this.presentLoading(loading); this.tw.login() .then( res => { // Get user data // There is a bug which fires the success event in the error event. // The issue is reported in https://github.com/chroa/twitter-connect-plugin/issues/23 this.tw.showUser() .then(user =>{ console.log(user); loading.dismiss(); }, err =>{ console.log(err); // default twitter image is too small https://developer.twitter.com/en/docs/accounts-and-users/user-profile-images-and-banners var profile_image = err.profile_image_url_https.replace('_normal',''); this.nativeStorage.setItem('twitter_user', { name: err.name, userName: err.screen_name, followers: err.followers_count, picture: profile_image }) .then(() => { this.router.navigate(["/user"]); loading.dismiss(); }, (error) => { console.log(error); loading.dismiss(); }) }) }, err => { loading.dismiss(); }) } async presentLoading(loading) { return await loading.present(); } }
Ionic Twitter Log Out
To perform Twitter log out we will call the tw.logout()
method that will invalidate the token generated at log in. Also, we will remove from the native storage the data from the logged user.
import { Component, OnInit } from '@angular/core'; import { UserModel } from './user.model'; import { TwitterConnect } from '@ionic-native/twitter-connect/ngx'; import { Router } from '@angular/router'; import { NativeStorage } from '@ionic-native/native-storage/ngx'; import { LoadingController } from '@ionic/angular'; @Component({ selector: 'app-user', templateUrl: './user.page.html', styleUrls: ['./user.page.scss'], }) export class UserPage implements OnInit { user: UserModel = new UserModel(); constructor( private tw: TwitterConnect, private nativeStorage: NativeStorage, public loadingController: LoadingController, private router: Router ) { } async ngOnInit() { const loading = await this.loadingController.create({ message: 'Please wait...' }); await loading.present(); this.nativeStorage.getItem('twitter_user') .then(data => { this.user = { name: data.name, userName: data.userName, picture: data.picture, followers: data.followers }; loading.dismiss(); }, error => { console.log(error); loading.dismiss(); }); } doTwLogout(){ this.tw.logout().then(response => { this.nativeStorage.remove('twitter_user'); this.router.navigate(["/login"]); }, error => { console.log(error); }); } }
Testing the functionalities
At this point you should have an Ionic application with Twitter log in and log out functionalities working. To test the native implementation you need to test the app on a real device or in an emulator because cordova isn't available in the browser. If you are new to Ionic development you can check how to Run your Ionic app in different environments.
Remember you can download all the source code of this Ionic example by clicking the GET THE CODE button from the beginning of this page.
Did you know that we recently released Ionic 5 Full Starter App? It's an ionic template that you can use to jump start your Ionic app development and save yourself hundreds of hours of design and development.
It is also ready to be a Progressive Web App and has a score of 100 in lighthouse. Try it on your phone as a PWA to see the magic!
Next Steps
After having Twitter authentication working in your Ionic v4 App you can focus on:
- Build a Progressive Web App with Ionic and Angular
- Add Google Login to an Ionic App.
- Add Facebook Authentication to an Ionic App.
- Forms and Validations in Ionic
- Add Social Sharing and Deep Linking
As you know we also sell beautiful ionic templates that you will find super useful as they save you hours of development, time and effort, while giving your projects a great design from scratch.
Why don’t you give a chance to our Ionic templates and apply this new knowledge?
Enjoyed reading this Ionic Tutorial?
Subscribe to keep learning Ionic Framework! You will receive offers, new ionic tutorials and free code examples from IonicThemes.