# Social Authentication in Text Express

## Analysis

> *Problem analysis, Literature study, Document analysis, Domain modelling, Task analysis, Best good and bad practices*

### Current situation

As Text Express is working with Playfab, in order to retrieve your progress from the service, the game identifies each player, by their unique device ID number. This is working perfectly, but is not secure at all. In case you lose your device, you would also lose your Text Express progress. In order to prevent this from happening and allow future social interaction between players, I have been given the following assignment.

### Graduation Assignment

As currently the game is being developed only for Android, an assignment for my graduation project was to implement Google and Facebook authentication services and allow players to safeguard their current progress as a first proof of concept that social authentication works.

### Clarifications

#### ***What are the best practices for Google or Facebook login, when using Playfab?***

In order to get a grasp on how the login should be done I've read the documentation of [Playfab for best practices](https://docs.microsoft.com/en-us/gaming/playfab/features/authentication/login/login-basics-best-practices#recoverable-login-mechanisms). Then I analyzed the following:

#### ***How does the login with Playfab currently works in Text Express?***

In order to understand how the login in Text Express currently works with Playfab, I've created this flow diagram:

![Flow diagram of how Playfab currently tackles login in Text Express ](/files/-MZHc6iYyrZrkn856OHv)

In the top flow diagram, the player (Luc) can not continue his progress from **Playfab ID "A",** on another device different than **Device 1**, when the game doesn’t support any authentication services.

As a conclusion from the research, when authentication services are supported, linking the service account to the Playfab ID account will be enough for Playfab to return the correct profile.

#### *Are there other games using Playfab, which have an authentication system?*

I've contacted a developer from [Bed and Breakfast](https://play.google.com/store/apps/details?id=com.gamehouse.deliciousmatch3\&hl=en\&gl=US) (Gamehouse game, which uses Playfab as a backend service) and understood that their game has only Facebook login. I've also received access to their codebase, where I analyzed their code for the Facebook login. Although, their code was just following the best practices from Playfab, I've found something I haven't thought about before:

#### ***What will happen if one Facebook account/ Google account is already linked and the player wants to link it on another Playfab ID?***

Whenever a player starts the game on a new phone, he already has a new Playfab ID account. If his older Playfab ID is already linked and authenticated with Google and then the player authenticates again on his newer phone (newer Playfab ID), then an error ([LinkedAccountAlreadyClaimed](https://docs.microsoft.com/en-us/rest/api/playfab/client/account-management/linkfacebookaccount?view=playfab-rest#linkfacebookaccountresult)) will be returned from Playfab. After [research ](https://community.playfab.com/questions/884/210404337-Login-flow-Lost-progress-and-unnecessary-created-account.html)about this case it was found out that this has to be tackled carefully by showing a "compare screen" to the player, comparing both of the Playfab ID accounts and asking the player which one he wants to continue playing.

#### *What to use from Google- Google email or google play account?*

There are 2 options you can use for Google login: A simple login with your *google email* or login with a Google Play account. After research here are the pros and cons of each:

|                         |                                Advantages                                |                                                      Disadvantages                                                     |
| ----------------------- | :----------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------: |
| **Google email**        |            <p>Simple Login with no <br>extra requirements</p>            |                              <p>Doesn't support automatic </p><p>login feature.</p><p></p>                             |
|                         |               <p>Could be used on all </p><p>platforms</p>               |                                                                                                                        |
|                         |                                                                          |                                                                                                                        |
|                         |                              **Advantages**                              |                                                    **Disadvantages**                                                   |
| **Google Play account** | <p>Supports automatic login</p><p>if you have played the game before</p> |                  <p>Requires Google Play app<br>installed on your phone </p><p>(Extra application)</p>                 |
|                         |                                                                          |                                             Can not be used on IOS devices                                             |
|                         |                                                                          | <p>Slows down the start of the game</p><p>by 0.5 - 2 seconds, so it checks </p><p>if you have authenticated before</p> |

After gathering and showing those to my design and technical mentor, they preferred that I use the Google Play account type of login, as it will have the automatic login feature, and most importantly because of  the following reason:&#x20;

> "*When a game uses as much as possible dedicated Google features (Google Ads, Google login, Google payments, etc), the chances of getting featured by the Google store are bigger.*" (Tjien- Text Express Designer)

#### ***How to set up the Google and Facebook SDK?***

In order to set up the[ ](https://developers.facebook.com/docs/unity)authentication services successfully, after research of what to do, I have created a list of tasks to follow:&#x20;

* [x] Creating a developer account on both platforms([Facebook ](https://developers.facebook.com/docs/unity/gettingstarted)and [Google](https://github.com/playgameservices/play-games-plugin-for-unity)):
* [x] Setting up the app in both platforms' consoles ([Facebook Console](https://developers.facebook.com/apps/?show_reminder=true) and [Google Console](https://play.google.com/console/)).
* [x] Setting up Playfab to recognize the authentication service ([Google ](https://docs.microsoft.com/en-us/gaming/playfab/features/authentication/platform-specific-authentication/google-sign-in-unity#playfab-google-sign-in-setup)and [Facebook](https://docs.microsoft.com/en-us/gaming/playfab/features/authentication/platform-specific-authentication/facebook-unity)).
* [x] Integrating the SDKs ([Google ](https://github.com/playgameservices/play-games-plugin-for-unity)and [Facebook](https://developers.facebook.com/docs/unity)) in Text Express's code and testing them for correct connection.

## Design

> *Design pattern research, Decomposition, IT Architecture Scetching, Explore user requirements, Brainstorming, Prototyping*

### Login with authentication service design

By keeping in mind the [analysis ](/social-and-community-features-in-text-express/features/social-authentication-in-text-expess.md#clarifications)I've done, I've created a new flow diagram, which demonstrates the login flow with an authentication system available for the players and also designing how this system will work technically-wise. More about this can be read [here](/social-and-community-features-in-text-express/research/design-for-login-with-authentication-flow.md).

{% content-ref url="/pages/-MZRUjvVVkaElWhT5ZNK" %}
[Design for login with authentication flow](/social-and-community-features-in-text-express/research/design-for-login-with-authentication-flow.md)
{% endcontent-ref %}

### Class design

All authentication services should have the same functionality: *Log in the service*, *Link/Unlink the service to Playfab, Log off the service, etc*. After ascertaining that, I've designed the following class diagram following SOLID principles- **Open-closed principle**, **Single responsible principle**:

![](/files/-MZUFOHDdCr-K4v4nvaG)

## Realization

> *Design pattern research, IT Architecture scetching, Code review, Usability testing*

### Setting up Text Express to use authentication services

Having to set up Text Express to allow Facebook authentication was just following my step-by-step guide (which I prepared in my [Analysis phase](/social-and-community-features-in-text-express/features/social-authentication-in-text-expess.md#how-to-set-up-the-google-and-facebook-sdk)) without having any major setbacks during the process. Facebook has done a great job in keeping their guides up-to-date and having their Unity SDK being very optimized, readable and easy to work with.

Setting up Text Express to have Google authentication, on the other hand, was a very different experience shared here:

{% content-ref url="/pages/-MZWhGgbKTGpPYc1EIsS" %}
[Setting up Text Express for Google Play Services](/social-and-community-features-in-text-express/research/set-up-text-express-in-the-google-platform.md)
{% endcontent-ref %}

### Authentication flow implementation:

After setting up Text Express on the Google and Facebook services and testing if their SDKs initializesuccessfully I started following the [class design](/social-and-community-features-in-text-express/features/social-authentication-in-text-expess.md#class-design) and the [login flow design ](/social-and-community-features-in-text-express/research/design-for-login-with-authentication-flow.md)I've created beforehand.&#x20;

#### Comparing Screen:

After I have successfully followed my design, I've reached the point where I have to present to the player two Playfab ID progresses- Comparing Screen. At first, I tested if it works, by using the following GUI.

![First proof of concept that comparing 2 Playfab IDs work](/files/-MXYjR4_2topPKPpzjhb)

After that I asked my design mentor, to decide what type of progress it is good to be shown in that comparing screen, so it is more comprehensible for the player. After several iterations the final GUI was agreed upon.

![Iteration of improving the comparing screen](/files/-MZcDmXKoKDY79albAUe)

Every change to the comparing screen meant that the backend function, should be changed to return the needed information. Which meant, that during those iterations, the backend function was updated as well, resulting in its final version:

{% file src="/files/-MZcJqLpHSqy\_r5wPmXW" %}
Final iteration- Backend function returning the progress
{% endfile %}

### Error handler

During the implementation of the authentication feature, a lot of possible errors had to be taken into account and all of them had to be handled differently. In the end, that resulted in implementing an [Error handler system](/social-and-community-features-in-text-express/research/errors-handler-in-text-express.md), which made the tracking of errors and fixing them faster and easier:

{% content-ref url="/pages/-MbwvAJZMxVkFjjeHLCm" %}
[Errors handler in Text Express](/social-and-community-features-in-text-express/research/errors-handler-in-text-express.md)
{% endcontent-ref %}

### Testing:

#### Personal testing

In order to test the authentication feature I had to constantly simulate 2 Playfab IDs. For that I used my laptop as one device and my personal phone as the other device. Facebook allows its SDK to be used on a PC, so testing the Facebook login functionality was easy and done pretty fast.&#x20;

Unfortunately, the Google SDK, can be used only on an android device and I was not able to test the Google login functionality thoroughly, as I don't have 2 android devices available. Therefore, I had to push the Google functionality halfway tested, hoping that it will work correctly.&#x20;

#### After QA testing:

After the build was tested by the QA testers, I've received information that the login with Google was not working as expected. In a 1 on 1 session with a QA tester I explained and guided, how to use Android Studio to read debug messages from the game. By seeing the error code of the GUI error appearing on the tester device and using an android studio to read the debug of the game, we managed to track what the issue was:

**Problem with requesting Google linked Playfab progress:**

The Playfab SDK client-side function [**GetPlayfabIDsFromGoogleIDs**](https://docs.microsoft.com/en-us/rest/api/playfab/client/account-management/getplayfabidsfromgoogleids?view=playfab-rest), was not returning the Playfab ID and it turned out that this was because the Google SDK doesn't support giving a Google ID, which is the parameter needed for the [**GetPlayfabIDsFromGoogleIDs**](https://docs.microsoft.com/en-us/rest/api/playfab/client/account-management/getplayfabidsfromgoogleids?view=playfab-rest). As this function was not an option I found another way of retrieving the playfabID of the player, which worked as intended:

![Changed on how the progress of another Playfab ID account is requested](/files/-MZT1baCFkt-xjBLAZ7T)

### Additional products:

The code for the authentication service was done and working in a modular way. I've decided to share all my knowledge with the rest of the Gamehouse developers, by making the following document, explaining how they can use my code in order to achieve the same authentication functionality as Text Express in their game as well.

{% file src="/files/-MZbCgu3DVcc9R7fWRfP" %}
Shared document with Gamehouse developers
{% endfile %}

![Receiving "thumbs ups" for the shared document :) ](/files/-MZSr-r5xgxgfkYdk_uh)

![More fuel to keep going <3](/files/-MZtI9qiBQRiEyKIrctv)

## Evaluation

> *Brainstorming*

Until now there was not an option for the players to safeguard their current progress and continue on their Text Express progress on more than one device. Now this is possible via the Facebook and Google authentication feature I've implemented.&#x20;

This safeguarding feature is believed to reduce the amount of people dropping out of the game, because buying a new phone and not wanting to start all over again.

The Text Express designer also believes that having social authentication such as Google or Facebook in your game is the first step in creating a social community around the game, as it allows the implementation of Clans/Guilds/Chat systems/Personal Profile/etc.   &#x20;

### Classes implementation

The authentication system was created following the fundamental SOLID programming principles and allowed me to see how powerful those can be. As I am trying to be as useful and helpful as possible, I've also shared the knowledge, logic and code-base of the authentication feature with fellow technical developers within Gamehouse. This act was appreciated, for which I am very grateful!

As proof that the code design is good, other members had to add tracking Business intelligence events. Those events were to track authentication activity and to gather data if and when the users are using the feature. In order to do that, the developer, who found easily the places, where he needed to add the code. Which for me suits as proof that the code is readable and you can understand it easily.

### For the future:

#### Automated Account Recovery

Due to drops in the players' internet connection, while some authentication operations are running, the player's account may be lost. Recovery of such account loss is now done only through contacting customer support (see "**Error nr. 8"** from the [**"Error handling when auth fails"** document](/social-and-community-features-in-text-express/features/social-authentication-in-text-expess.md#errors-controller)). However, there have been added tracking events to check, if "account lost"s are happening or not. If it is something regular, then an improvement of the code could be done, so the following automatic recovering is happening:

*The last logged-in playfab ID, which was on that phone is being linked back whenever the phone has internet again. This process could be automated, and not require contacting customer support, but currently is not, as I think it will be a very rare case (data will show).*

**Apple authentication**

When Text Express is available for IOS devices as well, Apple authentication would have to be added as well.

{% content-ref url="/pages/-MXRSeu5fcwD2asueoVs" %}
[Deeplinks in Text Express](/social-and-community-features-in-text-express/features/deeplinking.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lyudmil.gitbook.io/social-and-community-features-in-text-express/features/social-authentication-in-text-expess.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
