Managing User Identities

Prerequisite

Attach Facebook Identity to the User

In order 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.

Important

GetSocial is compatible with Facebook Android SDK v4.x, older versions are not supported.

  1. Integrate Facebook Android SDK into your app as described in the Official Guide.

  2. Copy implementation of the Facebook Invite plugin from GetSocial GitHub repository into your project.

  3. Register plugin with GetSocial:

    1
    2
    3
    4
    5
    CallbackManager facebookCallbackManager = CallbackManager.Factory.create();
    
    GetSocial.registerInviteProviderPlugin(
        InviteProviderPluginIds.FACEBOOK,
        new FacebookInvitePlugin(this, facebookCallbackManager));
    

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
42
43
44
45
46
47
48
49
50
51
52
53
54
final AccessTokenTracker accessTokenTracker = new AccessTokenTracker()
{
    @Override
    protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken)
    {
        stopTracking();

        AuthIdentity authIdentity = AuthIdentity.createFacebookIdentity(newAccessToken.getToken());
        GetSocial.User.addAuthIdentity(authIdentity, new AddAuthIdentityCallback() {
            @Override
            public void onComplete() {
                // GetSocial.User now contains the user with the latest identity.
                Log.d(TAG, "AddAuthIdentityCallback.onComplete for facebook\t");
                Log.d(TAG, "on Complete identities\t"+printProviders());
            }

            @Override
            public void onFailure(GetSocialException e) {
                // Ooops. There was some exception while adding User's identity.
                // Usually, most of the developers just show a dialog with "Please try once again" message.
                Log.d(TAG, "AddAuthIdentityCallback.onFailure FB \t"+e);
            }

            @Override
            public void onConflict(ConflictUser conflictUser) {
                // 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, new CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        // Update username, avatar or whatever you have on UI.
                    }

                    @Override
                    public void onFailure(GetSocialException e) {
                        // Ooops. There was some exception while switching the User.
                        // Usually, most of the developers just show a dialog with "Please try once again" message.
                    }
                });
                Log.d(TAG, "AddAuthIdentityCallback.onConflict FB \t"+conflictUser);
            }
        });
    }
};
accessTokenTracker.startTracking();

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_friends"));

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 don’t 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 login with the same user on several devices, you have to provide the same combination of Provider Name / User Id / Token.

We suggest to build 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
32
33
34
35
36
37
38
39
40
41
AuthIdentity authIdentity = AuthIdentity.createCustomIdentity("custom_auth_method", "user0001", "password-salt-00010002");
GetSocial.User.addAuthIdentity(authIdentity, new AddAuthIdentityCallback() {
    @Override
    public void onComplete() {
        // GetSocial.User now contains the user with custom identity.
        // Update username, avatar or any related UI elements.
        Log.d(TAG, "Custom identity added successfully.");
    }

    @Override
    public void onFailure(GetSocialException e) {
        // Ooops. There was some exception while adding User's identity.
        // Developers usually retry or show a dialog with "Please try once again" message.
        Log.d(TAG, "Custom identity was not added due to exception: " + e);
    }

    @Override
    public void onConflict(ConflictUser conflictUser) {
        // 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, new CompletionCallback() {
            @Override
            public void onSuccess() {
                // Update username, avatar or any related UI elements.
            }

            @Override
            public void onFailure(GetSocialException e) {
                // 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
 8
 9
10
11
12
String providerId = "custom_auth_method"; // Tip: use "facebook" to remove FB identity
GetSocial.User.removeAuthIdentity(providerId, new CompletionCallback() {
    @Override
    public void onSuccess() {
        Log.d(TAG, "Identity successfully removed.");
    }

    @Override
    public void onFailure(GetSocialException e) {
        Log.d(TAG, "Failed, while removing identity.");
    }
});

Reset User

To reset current user and create new anonymous one, use GetSocial.User.reset(callback) 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, callback) method.

Give us your feedback! Was this article helpful?

😀 🙁