Managing User Identities

Prerequisite

Attach Facebook Identity to the User

To add Facebook identity, you should provide the token you got from Facebook SDK after user’s authentication.

Don’t forget to sync the display name and avatar of the GetSocial user with the values from the added social network identity.

For Facebook, we will retrieve the user’s list of friends and import them so that it can be used later through our Social Graph feature.

Let’s add Facebook Identity to the user. Sample code is below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
FB.Init(() => {
    var permissions = new List<string>(){"public_profile", "user_friends"};
    FB.LogInWithReadPermissions(permissions, result => {
        if (FB.IsLoggedIn) {
            var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
            var authIdentity = AuthIdentity.CreateFacebookIdentity(aToken.TokenString);
            GetSocial.User.AddAuthIdentity (authIdentity, 
                () => {
                    // GetSocial.User now contains the user with custom identity.
                    // Update username, avatar or any related UI elements.
                    Debug.Log ("Identity added successfully.");
                },
                error => {
                    // Ooops. There was some exception while adding User's identity.
                    // Developers usually retry or show a dialog with "Please try once again" message.
                    Debug.Log ("Custom identity was not added due to exception: " + error);
                },
                conflict => {
                    // So this means, there's already a user, which logged in with the same credentials before.
                    // This is usually the case when a person has 2 or more devices. E.g., phone and tablet.

                    // Usually, you would compare `conflictUser` to the current one and choose one of the 2 options:

                    // Option 1. Keep existing user in place.
                    // In this case, just ignore this method and return.

                    // Option 2. Switch to the already existing user via calling `GetSocial.User.switchUser(...)`.
                    GetSocial.User.SwitchUser (authIdentity,
                        () => {
                            // Update username, avatar or any related UI elements.
                        },
                        error => {
                            // Ooops. There was some exception while switching the User.
                            // Developers usually retry or show a dialog with "Please try once again" message.
                        });
                });
        } else {
            Debug.Log("User cancelled login");
        }
    });
});

To understand better the concept of a User Identity conflict, please refer to Handling Multiple User’s Devices.

Attach Custom Identity to the User

If you have a proprietary login method that you already use on your App or any other provider that we do not currently support natively (i.e. Game Center, Twitter, etc.), you can also identify your users with it.

In this case, you will need to provide a Provider Name, a User Id and a Token.

The Provider Name is a unique string identifier of the provider, e.g. “twitter”, “email”, “custom_auth_method”.

The User Id is the unique string identifier of the user on the specified provider.

The Token is a custom string, linked to the user, that you will need to provide to be able to recover this user later if needed.

To log in with the same user on several devices, you have to provide the same combination of Provider Name / User Id / Token.

We suggest building the Token using a hash of the User Id and a shared secret.
This way, you can easily regenerate the token if the user adds the identity on another device.

Don’t forget to sync the display name and avatar of the GetSocial user with the values from your system.

Here’s the code sample for adding custom provider for your own identity system:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var authIdentity = AuthIdentity.CreateCustomIdentity ("custom_auth_method", "user0001", "password-salt-00010002");
GetSocial.User.AddAuthIdentity (authIdentity, 
    () => {
        // GetSocial.User now contains the user with custom identity.
        // Update username, avatar or any related UI elements.
        Debug.Log ("Identity added successfully.");
    },
    error => {
        // Ooops. There was some exception while adding User's identity.
        // Developers usually retry or show a dialog with "Please try once again" message.
        Debug.Log ("Custom identity was not added due to exception: " + error);
    },
    conflict => {
        // So this means, there's already a user, which logged in with the same credentials before.
        // This is usually the case when a person has 2 or more devices. E.g., phone and tablet.

        // Usually, you would compare `conflictUser` to the current one and choose one of the 2 options:

        // Option 1. Keep existing user in place.
        // In this case, just ignore this method and return.

        // Option 2. Switch to the already existing user via calling `GetSocial.User.switchUser(...)`.
        GetSocial.User.SwitchUser (authIdentity,
            () => {
                // Update username, avatar or any related UI elements.
            },
            error => {
                // Ooops. There was some exception while switching the User.
                // Developers usually retry or show a dialog with "Please try once again" message.
            });
    });

To understand better the concept of a User Identity conflict, please refer to Handling Multiple User’s Devices.

Remove User Identity

You can remove an identity you have previously added to the GetSocial user.

After removing all identities of the GetSocial user, it will become anonymous.

The user’s display name and avatar will remain the same.

1
2
3
4
5
6
7
GetSocial.User.RemoveAuthIdentity ("custom", // Tip: use AuthIdentityProvider.Facebook to remove FB identity
    () => {
        Debug.Log ("Identity successfully removed.");
    },
    error => {
        Debug.Log ("Removing identity failed, error: " + error);
    });

Reset User

To reset current user and create new anonymous one, use GetSocial.User.Reset(onSuccess, onError) method.
If current user is anonymous, it will be lost. If it had at least one identity, you still can login into that account using GetSocial.User.SwitchUser(identity, onSuccess, onError) method.

Give us your feedback! Was this article helpful?

😀 🙁