Skip to content

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
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorBrowser;

[login
    logInWithReadPermissions:@[@"email", @"user_friends"]
    fromViewController:self
    handler:^(FBSDKLoginManagerLoginResult *result, NSError *loginError) {
    if (!loginError && !result.isCancelled)
    {
        GetSocialAuthIdentity *identity = [GetSocialAuthIdentity facebookIdentityWithAccessToken:result.token.tokenString];

        [GetSocialUser addAuthIdentity:identity
                                success:^{
                                    // GetSocialUser now contains the user with custom identity.
                                    // Update username, avatar or any related UI elements.
                                    NSLog(@"User identity %@ added, result: Identity added", identity);
                                } conflict:^(GetSocialConflictUser *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 `[GetSocialUser switchUser...`.
                                    [GetSocialUser switchUserToIdentity:identity success:^{
                                        // Update username, avatar or any related UI elements.
                                    } failure:^(NSError *error) {
                                        // Ooops. There was some exception while switching the User.
                                        // Developers usually retry or show a dialog with "Please try once again" message.
                                        NSLog(@"User switching failed, error: %@", [error description]);
                                    }];
                                } failure:^(NSError *error) {
                                    // Ooops. There was some exception while adding User's identity.
                                    // Developers usually retry or show a dialog with "Please try once again" message.
                                    NSLog(@"Failed to add user identity %@, error: %@", identity, [error localizedDescription]);
                                }];
    }
}];

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
GetSocialAuthIdentity *identity = [GetSocialAuthIdentity customIdentityForProvider:@"custom_auth_method" userId:@"user0001" accessToken:@"password-salt-00010002"];

[GetSocialUser addAuthIdentity:identity
                        success:^{
                            // GetSocialUser now contains the user with custom identity.
                            // Update username, avatar or any related UI elements.
                            NSLog(@"User identity %@ added, result: Identity added", identity);
                        } conflict:^(GetSocialConflictUser *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 `[GetSocialUser switchUser...`.
                            [GetSocialUser switchUserToIdentity:identity success:^{
                                // Update username, avatar or any related UI elements.
                            } failure:^(NSError *error) {
                                // Ooops. There was some exception while switching the User.
                                // Developers usually retry or show a dialog with "Please try once again" message.
                                NSLog(@"User switching failed, error: %@", [error description]);
                            }];
                        } failure:^(NSError *error) {
                            // Ooops. There was some exception while adding User's identity.
                            // Developers usually retry or show a dialog with "Please try once again" message.
                            NSLog(@"Failed to add user identity %@, error: %@", identity, [error localizedDescription]);
                        }];

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
NSString *providerId = @"custom_auth_method"; //Tip: use GetSocial_AuthIdentityProviderId_Facebook to remove FB identity
[GetSocialUser removeAuthIdentityWithProviderId:providerId success:^{
    NSLog(@"Identity successfully removed.");
} failure:^(NSError *error) {
    NSLog(@"Removing identity failed, error: %@", [error description]);
}];

Reset User

To reset current user and create new anonymous one, use [GetSocialUser resetWithSuccess:success failure:failure] 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 [GetSocialUser switchUserToIdentity:identity success:success failure:failure] method.

Give us your feedback! Was this article helpful?

😀 🙁