If your mobile application will require users to authenticate in order to access content, then you can either build your own authentication mechanism in Kumulos or you can use Kumulos to integrate Facebook Login or the Google Identity Platform to support social login for your users. This recipe will show you how you can write a KScript to allow your users to login with their Facebook credentials and then use the Facebook Graph API to retrieve details about them.

Ingredients

This recipe assumes that you have KScripts enabled for your app, you have a Facebook developer account and have registered your app.

Recipe

Kscripts includes a Facebook SDK for use within your Kscript methods. It requires a valid Facebook access token in order to initialize for use.

The access token is retrieved by integrating a Facebook SDK into your app and then following the Facebook login guide.

Once initialized, you can then call the Facebook Graph API from your KScript methods to access the users profile. A sequence diagram illustrating an example integration is as follows:

Facebook Integration Sequence Diagram

Click the 'Create Some KScript' button and create a new KScript method called setFacebookAccessToken to upload the user access token and save this in the KScript session.

K.session.fbAccessToken = K.params.fbAccessToken;

Click the 'Create Some KScript' button and create a new KScript method called _facebook_ to encapsulate the initialization of the Facebook SDK.

var FB_APP_ID = '';
var FB_APP_SECRET = '';
// Assume session storage of access token set by setFacebookAccessToken
K.initFB(FB_APP_ID, FB_APP_SECRET, K.session.fbAccessToken);

Finally, click the 'Create Some KScript' button and create a new KScript method called getUserProfile to make an API call to the Facebook Graph API.

include('_facebook_');
var userProfile = K.FB.api('/me');
K.log(userProfile);
K.setResponse(userProfile);

A similar process can be implemented to integrate the Google Identity Platform]

Further reading

A full reference guide to the Facebook Graph API can be found on the Facebook Developer Site. Some methods may require the Facebook User ID. This can be retrieved via the K.FB.getUser() method (for which further information can be found in the KScript Reference Guide).