October 4, 2016 7:50 am

Social media logins with PhoneGap

PhoneGap is a tool that can help you make hybrid applications for different devices such as Android and iOS using HTML, CSS and JavaScript. However, there are a few differences when compared to normal websites and native apps. You would have to use plugins to do tasks that traditional websites do with ease – for example, to upload a file you would have to use the File Transfer plugin and use a different API than adding inputs with a type attribute set to file.

Social media logins with PhoneGap

The concept is that you would use HTML,CSS and JavaScript to create your application but you would have to approach things a bit differently to get more complicated apps up and running. To create a log in system with different social networks is one example.

In this brief tutorial, we are going to show you how to login the user with Google, Facebook and LinkedIn within a PhoneGap application.

In traditional web apps, you would just redirect the user to the social network’s authentication page, and then the users would be returned to one of your pages. However, with PhoneGap you do not have any URL to return them to. That is why one approach to handle this is to use the PhoneGap’s InAppBrowser plugin which is a browser with some added hooks. Using this plugin, you can redirect them to any page and set up listeners for when a new page loads in the browser. When the new page loads, you can check if the URL matches the URL of the success page that you set in the request for authentication with the social network – and if it matches – you can then search for an access token within the URL which you can afterwards use to get user data when needed.

A drawback of this approach is that your client secret key with the social network may be hardcoded within the PhoneGap application. If a user examines your code, he/she can find it and present himself/herself as your application, which is a situation you would have to reflect upon.

The code below relies on jQuery and the PhoneGap InAppBrowser plugin.

To login with Facebook, there is a method that takes the Facebook’s app id and app secret which you can get by creating a new app at: https://developers.facebook.com/apps/ and a success callback which will be called with the access token if the user has been successfully logged in.  The function itself opens the Facebook authentication page with the InAppBrowser and sets up listeners for when a page loads. When a page loads, it checks if the URL matches the redirect_uri parameter and if there is an access token. If that is the case – we know that the user has been successfully authenticated, so we parse the access token and call the callback. We also save the token in localStorage for later usage.

The situation is very similar with LinkedIn. We do basically the same things but change the URLs.

With Google, nothing substantial changes:

Now, if you want to get the user’s profile information, you can use the token that we have saved to localStorage. For example, to get the user’s profile information with Google, we just need to make a request to the URL shown below and pass it the saved access token.

To get the user’s Facebook details, the approach is the same; there is no more need of InAppBrowser. We can make a request to their API endpoint, get the JSON data, and extract the information from their response that we need.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Tutorial Categories:
  • Rig Nortana

    can you share demo code for this ?