“As a player I want to sign in using my facebook account so that I can save time.”

Week Nine User Story Sprint

Introduction

From a player’s perspective, signing up for an app on a mobile device can be an offputting chore. It usually involves entering an email address and then having to verify it, which means having to login to webmail or even switching to a laptop or desktop computer to get to the validation message. Then there’s the password, different apps have different strength criteria which means it’s not always possible to keep the same password across all apps.

Facebook Login

One solution to this inconvenience problem of manual account creation and authentication, is to use social sign in to allow a user to authenticate their identity to an app via an existing, trusted provider. For example, Facebook Login – “a fast and convenient way for people to create accounts and log into your app across multiple platforms” (Facebook).

I decided to integrate Facebook Login into my LovelLock app and make it the default, mandatory authentication method for my first prototype. This means that my initial users must have an ative Facebook account to be able to use my app, but it makes the sign up and sign in process much more streamline. I don’t have to worry about validating email addresses and passwords during my prototype build stage so I can focus on more critical features instead.

While Facebook makes it easier for my end-users to get started with my app, it also provides additional marketing opportuities to me as the app’s owner. I can configure my app to request different types of permissions from my user. By requesting the user’s email and public profile, I can store their name and other details within my app for a more personalised experience. I can also request access to the user’s Facebook friends providing the potential for my users to invite others to join the app and even share their virtual lovelocks together in future releases.

Firebase Multi-Platform Sign In

Although I have decided that Facebook Login will be the only method for onboarding my initial users, I want to be able to cater, in future releases, for non Facebook users, too. This is why I chose to intgegrate my app with Firebase, a multi-platform sign in system by Google. Firebase Authentication is a cloud based identity solution that I can use to keep all my users tracked in one place regardless of whether they use Facebook, Twitter, GitHub or standard email/password to sign in to my app. By setting up Firebase and Facebook sign-in from the outset I can easily add further authentication methods further down the line.

Unity Integration

Facebook Login and Firebase Authentication each provide Software Development Kits for Unity, the game engine I am using to build my app.

Facebook SDK for Unity

Getting Started with Firebase Authentication in Unity

I added both SDK’s to my project’s assets folder within Unity. I then had to register my app with Facebook and Firebase to obtain my API keys which are used to authorise my app for access to their services. I then created scripts within my app’s main code to interact between my app’s user interface and the two SDK’s.

public static void FbLogin()
        {
            if (FB.IsLoggedIn)
            {
                var accessToken = AccessToken.CurrentAccessToken.TokenString;
                FirebaseService.FireBaseFacebookLogin(accessToken);
            }
            else
            {
                var perms = new List<string> {"public_profile", "email"};
                FB.LogInWithReadPermissions(perms, AuthCallback);
            }
        }

The above code segment shows a static method which checks if a user is already authenticated via Facebook. If they are, I pass an accessToken, provided by Facebook, to Firebase. If the user is not yet authenticated via Facebook I initialise the login process by calling the LogInWithReadPermissions method from the Facebook SDK. Assuming the user accepts my app’s request to access their email and public_profile a new accessToken is issued which is then sent to Firebase.

It is possible to simulate the Facebook Login process within Unity without having to deploy the app to a mobile device.

Facebook Signin Within Unity

Using Firebase Auth SDK means that I can track my app user’s authentication status within a single function, no matter which method was used to login the user.

  private void LoadSignIn()
        {
            Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                var dependencyStatus = task.Result;
                if (dependencyStatus == Firebase.DependencyStatus.Available) 
                {
                    _auth.StateChanged += AuthStateChanged;
                    new SignInFactory().Load(view);
                }
                else
                {
                    UnityEngine.Debug.LogError(System.String.Format(
                        "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                    // Firebase Unity SDK is not safe to use here.
                }
            });
            
        }

The above code is executed each time the app is opened. The app first checks that all Firebase dependencies are installed and working correctly before loading the Log In screen via the SignInFactory() method.

Firebase Authentication provides a status change event to which my app is able to subscribe. Once a user is successfuly authenticated this event will fire a status change. I check that the Firebase Authentication object contains a valid user before replacing the Sigin In screen with the main app menu and personalised welcome message.

private void AuthStateChanged(object obj,EventArgs args)
        {
            if(obj is FirebaseAuth)
            {
                FirebaseAuth auth = obj as FirebaseAuth;
                if (auth.CurrentUser!=null)
                {
                    Debug.Log("is signed in, send to Main");
                    SceneManager.LoadScene("Home");
                }
                
            }
         
        }

Firebase Database and Analytics

Firebase is great for providing a way of allowing my users to sign in to my app using a range of social sign in platforms including Facebook, Twitter and so on. However, by integrating Firebase into my app I will also be able to take advantage of other features including analytics about performance and crashes, as well as data storage which I will be using to store data about my user’s Virtual Lovelocks such as their geographical coordinates and media attachments.

Comments

Leave a Reply