Login and Logout Users

Introduction

If you have configured your application, you can start using GetSocial without any additional steps. GetSocial creates anonymous user by default, so you can access all the features right after SDK gets initialized.

Anonymous user is just a user, that has no identities added. User is created per device and stays there for next sessions. Anonymous users have default names like “User 12345678”, but you can easily change it.

To check if user is anonymous:

1
val isAnonymous = GetSocial.getCurrentUser()?.isAnonymous
1
let anonymous = GetSocial.currentUser().isAnonymous()
1
var anonymous = GetSocial.GetCurrentUser().IsAnonymous;
1
2
var currentUser = await GetSocial.currentUser;
var isAnonymous = currentUser.isAnonymous;

Login

If you have internal login system or use any social authentication, you should connect your account to GetSocial user, so you can retrieve the same GetSocial user from another devices or recover it once app was reinstalled or user was logged out.

To connect GetSocial user with yours, you should add your identity to GetSocial user.

Identities

An identity is some unique user data to identify them among other users. You can use any auth provider to create an identity (your custom login, facebook, twitter, google, github, etc). Each user may have any number of different identities, but only one of the same provider. It means that you may attach one facebook, one twitter and one custom identity, but you can not attach two facebook identities. Read more how to attach multiple identities.

To create an identity:

1
2
3
4
5
// Create Facebook identity
val fbIdentity = Identity.facebook(fbAccessToken)

// Create Custom Identity
val custom = Identity.custom(providerId, userId, accessToken)
1
2
3
4
5
// Create Facebook identity
let fbIdentity = Identity.facebook(accessToken: fbAccessToken)

// Create Custom Identity
let custom = Identity.custom(providerId: providerId, userId: userId, accessToken: accessToken)
1
2
3
4
5
// Create Facebook identity
var fbIdentity = Identity.Facebook(fbAccessToken);

// Create Custom Identity
var customIdentity = Identity.Custom(providerId, userId, accessToken);
1
2
3
4
5
// Create Facebook identity
var fbIdentity = Identity.facebook(fbAccessToken);

// Create Custom Identity
var customIdentity = Identity.custom(providerId, userId, accessToken);
  • providerId is a unique string defined by you for each provider (twitter, my_auth_system, gamecenter, etc).
  • userId is a unique UserId for each user in the provided authentication system. So pair providerId-userId is unique for each user and helps GetSocial identify a user if them try to login from another device.
  • accessToken is a string for security check, that could be some internal hash function in your app based on userId. If user is trying to authenticate again with the same providerId and userId, but different accessToken - GetSocial won’t allow to authenticate. accessToken can not be changed.

Add Identity

You have to add an identity to GetSocial user in the success callback of your authentication 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
fun onLoginSuccess() {
    val userId = getCurrentUserId() // get user ID on your login provider
    val accessToken = calculateAccessTokenForUser(userId) // see the example of such a function below

    val identity = Identity.custom("my_auth_system", userId, accessToken)
    val currentUser = GetSocial.getCurrentUser()

    if (currentUser == null) {
        // you can't add identity before SDK is initialized
        return
    }

    currentUser.addIdentity(identity, {
        Log.d("CurrentUser", "Identity added successfully")
    }, { conflictUser: ConflictUser ->
        handleConflic(identity, conflictUser)
    }, { error: GetSocialError ->
        Log.d("CurrentUser", "Failed to add identity: $error")
    })
}

fun calculateAccessTokenForUser(userId: String): String {
    return ... // you should implement your function to be identical on all platforms
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
let userId = getCurrentUserId() // get user ID on your login provider
let accessToken = calculateAccessToken(userId) // see the example of such a function below
let identity = Identity.custom(providerId: "my_auth_system", userId: userId, accessToken: accessToken)
GetSocial.currentUser()?.addIdentity(identity, success: {
    print("Successfully logged into \(userId)")
}, conflict: { conflictUser in
    self.handleConflict(identity: identity, conflictUser: conflictUser)
}, failure: { error in
    print("Failed to log into \(userId)")
})

func calculateAccessToken(userId: String) -> String {
    return ... // you should implement your function to be identical on all platforms
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
var userId = GetCurrentUserId(); // get user ID on your login provider
var accessToken = CalculateAccessToken(userId); // generate access token. make sure token generation code returns the same result on every platform.
var identity = Identity.Custom("my_auth_system", userId, accessToken);
GetSocial.GetCurrentUser().AddIdentity(identity, () => {
    Debug.Log("Successfully logged into " + userId);
}, (conflictUser) => {
    HandleConflict(conflictUser);
}, (error) => {
    Debug.Log("Failed to log into " + userId);
});

string CalculateAccessToken(string userId)
{
    return ... // make sure token generation code returns the same result on every platform
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
var userId = getCurrentUserId();  // get user ID on your login provider
var accessToken = calculateAccessToken(); // generate access token. make sure token generation code returns the same result on every platform.
var identity = Identity.custom('my_auth_system', userId, accessToken);
var user = await GetSocial.currentUser;
user.addIdentity(
    identity,
    () => { print('Successfully logged into ' + userId)},
    (conflictUser) => { handleConflict(conflictUser) },
    (error) => { print('Failed to log into ' + userId)});
}

String calculateAccessToken(String userId) {
    return ... // make sure token generation code returns the same result on every platform
}

Add Facebook Identity

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

  1. Integrate Facebook SDK into your app as described in the Official Guide for iOS, Android or Unity.

  2. Add Facebook identity to GetSocial user. Sample code is below:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    val accessTokenTracker = object: AccessTokenTracker() {
        override fun onCurrentAccessTokenChanged(oldAccessToken: AccessToken, currentAccessToken: AccessToken) {
            stopTracking() // stop tracking facebook access token changes as we don't need it anymore
    
            val currentUser = GetSocial.getCurrentUser()
    
            if (currentUser == null) {
                // you can't add identity before SDK is initialized
                // you can save the token to use it later
                accessToken = currentAccessToken
                return
            }
    
            currentUser.addIdentity(Identity.facebook(currentAccessToken), {}, {}, {})
        }
    }
    accessTokenTracker.startTracking()
    
    LoginManager.getInstance().logInWithReadPermissions(this@MainActivity, listOf("email", "user_friends")) // we need "user_friends" permission to import list of Facebook friends to GetSocial Social Graph
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    let login = FBSDKLoginManager()
    login.loginBehavior = FBSDKLoginBehaviorBrowser
    
    /* we need "user_friends" permission to import list of Facebook friends to GetSocial Social Graph */
    login.logIn(withReadPermissions: ["email", "user_friends"], fromViewController: self, handler: { result, loginError in
        if loginError == nil && result?.isCancelled == nil {
            let identity = Identity.facebook(accessToken: result?.token.tokenString)
    
            GetSocial.currentUser().addIdentity(identity, success: {
                print("Successfully logged into FB")
            }, conflict: { conflictUser in
                self.handleConflict(for: identity, conflictUser: conflictUser)
            }, failure: { error in
                print("Failed to log into FB")
            })
        }
    })
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    FB.Init(() => {
        var permissions = new List<string>(){"public_profile", "user_friends"}; // We need "user_friends" permission to import list of Facebook friends to GetSocial Social Graph
        FB.LogInWithReadPermissions(permissions, result => {
            if (FB.IsLoggedIn) {
                var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
                var identity = Identity.Facebook(aToken.TokenString);
                GetSocial.GetCurrentUser().AddIdentity (identity, OnSuccess, OnError, OnConflict);
            } else {
                Debug.Log("User cancelled login");
            }
        });
    });
    
    1
    2
    3
    4
    5
    6
    7
    8
    var fbIdentity = Identity.facebook(fbAcessToken);
    var user = await GetSocial.currentUser;
    user.addIdentity(
        fbIdentity,
        () => { print('Successfully logged into ' + fbIdentity)},
        (conflictUser) => { handleConflict(conflictUser) },
        (error) => { print('Failed to log into ' + fbIdentity)});
    }   
    

For Facebook, we will retrieve the list of Facebook friends and import them to Social Graph, so you can get list of user’s friends through GetSocial.

GetSocial doesn’t automatically sync your Facebook profile info

Don’t forget to sync the display name and avatar of the GetSocial user with the values from Facebook. You can do it with a batch update to make it in one call.

Handle Conflicts

Besides success and failure callbacks, addIdentity has conflict. Conflict happens when identity you’re trying to add is already attached to one of GetSocial users. It may happen when:

  • User has already logged in on another device.
  • User reinstalled the application.
  • User cleared application data.

Depending on the user intention and your application logic, you can choose one of the following strategies to resolve the conflict.

Strategy 1: stay with current user and don’t add identity:

1
2
3
4
fun handleConflict(identity: Identity, conflictUser: ConflictUser) {
    // Do nothing in onConflict and user will stay the same.
    // Identity won't be added to any of the users.
}
1
2
3
4
func handleConflict(identity: Identity, conflictUser: ConflictUser) {
    // Do nothing in onConflict and user will stay the same.
    // Identity won't be added to any of the users.
}
1
2
3
4
5
void HandleConflict(Identity identity, ConflictUser conflictUser)
{
    // Do nothing in onConflict and user will stay the same.
    // Identity won't be added to any of the users.
}
1
2
3
4
handleConflict(Identity identity, ConflictUser conflictUser) {
    // Do nothing in onConflict and user will stay the same.
    // Identity won't be added to any of the users.
}

Strategy 2: switch to conflict user:

1
2
3
4
5
6
fun handleConflict(identity: Identity, conflictUser: ConflictUser) {
    // Call switchUser to replace current user with conflict one and add identity to conflict user.
    // After the successful switch, current user and his referral data will be lost. 
    // To save the data: 1. copy data to intermediate variables; 2. save in to the conflict user properties after the successful switch.  
    GetSocial.User.switchUser(identity, { /** onSuccess **/ }, { /** onError **/ })
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func handleConflict(identity: Identity, conflictUser: ConflictUser) {
    // Call switchUser to replace current user with conflict one and add identity to conflict user.
    // After the successful switch, current user and his referral data will be lost.
    // To save the data: 1. copy data to intermediate variables; 2. save in to the conflict user properties after the successful switch.
    GetSocial.switchUser(to: identity, success: {
        print("Successfully switched user")
    }, failure: { error in
        print("Failed to switch user")
    })
}
1
2
3
4
5
6
7
void HandleConflict(Identity identity, ConflictUser conflictUser)
{
    // Call switchUser to replace current user with conflict one and add identity to conflict user.
    // After the successful switch, current user and his referral data will be lost. 
    // To save the data: 1. copy data to intermediate variables; 2. save in to the conflict user properties after the successful switch.
    GetSocial.SwitchUser(identity, completionCallback);
}
1
2
3
4
5
6
handleConflict(Identity identity, ConflictUser conflictUser) {
    // Call switchUser to replace current user with conflict one and add identity to conflict user.
    // After the successful switch, current user and his referral data will be lost. 
    // To save the data: 1. copy data to intermediate variables; 2. save in to the conflict user properties after the successful switch.
    GetSocial.switchUser(identity).then(...);
}

Logout

If your users can log out of your app, you should log out of GetSocial user too. Otherwise it will stay the same and will have connection with another user that will login after.

1
2
3
4
fun onLogoutSuccess() {
    // New anonymous will be created. If your current user is anonymous - it will be lost.
    GetSocial.resetUser({ /** onSuccess **/, /** onError **/ })
}
1
2
3
4
func onLogoutSuccess() {
    // New anonymous user will be created. If your current user is anonymous - it will be lost.
    GetSocial.resetUser(success: successCallback, failure: failureCallback)
}
1
2
3
4
5
void OnLogoutSuccess()
{
    // New anonymous will be created. If your current user is anonymous - it will be lost.
    GetSocial.ResetUser(OnSuccess, OnError);
}
1
2
3
4
onLogoutSuccess() {
    // New anonymous will be created. If your current user is anonymous - it will be lost.
    GetSocial.resetUser().then(...);
}

Subscribe to GetSocial user changes to be notified about logout events too.

Handle Multiple User Identities

You may want to connect multiple identities (login with Facebook and Google at the same time). It will allow to log into that user with different auth providers.

Your user may have any number of different identities attached for different login providers, but only one identity for each provider.

You already know how to add identity. Once it is successfully added, you can log in using that identity.

To remove identity from user:

1
2
3
4
5
6
7
8
val providerId = "my_auth_system"
val currentUser = GetSocial.getCurrentUser()
if (currentUser == null) {
    // you can't remove identity before SDK is initialized
    return
}

currentUser.removeIdentity(providerId, { /** onSuccess **/, /** onError **/ })
1
2
let providerId = "my_auth_system"
GetSocialUser.removeIdentity(providerId: providerId, success: success, failure: failure)
1
2
3
4
5
6
7
8
var providerId = "my_auth_system";
var currentUser = GetSocial.GetCurrentUser();
if (currentUser == null)
{
    // you can't remove identity before SDK is initialized
    return
}
currentUser.RemoveIdentity(providerId, OnSuccess, OnError);
1
2
3
4
5
6
7
var providerId = "my_auth_system";
var currentUser = await GetSocial.currentUser;
if (currentUser == null) {
    // you can't remove identity before SDK is initialized
    return
}
currentUser.removeIdentity(providerId).then(...);

To get all user identities:

1
val identities = GetSocial.currentUser()?.getIdentities()
1
let identities = GetSocial.currentUser()?.identities
1
var identities = GetSocial.GetCurrentUser().Identities;
1
2
var currentUser = await GetSocial.currentUser
var identities = currentUser.identities;

where key is providerId and value is userId for that provider.

Change Logged In User

If you want to switch from one logged in user to another, do Logout first and then Login with a new user.

Warning

Don’t forget to do the logout, it is important to keep your user connected with proper GetSocial user, so you will receive correct analytics and referral data.

Subscribe to User Lifecycle Changes

User change handler is invoked when:

Set your custom event handler somewhere on the start of your application:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Setup GetSocial
        val listenerId = GetSocial.addOnCurrentUserChangedListener { newUser: CurrentUser ->
            Log.d("CurrentUser", "Is user anonymous? ${newUser.isAnonymous}")
            Log.d("CurrentUser", "What's user's avatar? ${newUser.avatarUrl}")
            Log.d("CurrentUser", "What's user's name? ${newUser.displayName}")
            Log.d("CurrentUser", "List of user's identities: ${newUser.identities}")
        }
        ...

        // You can remove the listener later
        GetSocial.removeOnCurrentUserChangedListener(listenerId)
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class ViewController: UIViewController {
    ...
    func viewDidLoad() {
        super.viewDidLoad()
        ...
        let listenerId = GetSocial.addOnCurrentUserChangedListener { currentUser in
            print("User is anonymous: \(currentUser.isAnonymous ? "Yes" : "No")")
            print("User's displayName: \(currentUser.displayName)")
            print("User's avatarURL: \(currentUser.avatarUrl)")
            print("User's identites: \(currentUser.identities)")
        }
        ...

        // You can remove the listener later
        GetSocial.removeOnCurrentUserChangedListener(listenerId)
    }
}
1
2
3
4
5
6
7
8
9
var listenerId = GetSocial.AddOnCurrentUserChangedListener((currentUser) => {
    Debug.Log("User is anonymous: " + currentUser.IsAnonymous);
    Debug.Log("User's displayName: " + currentUser.DisplayName);
    Debug.Log("User's avatarURL: " + currentUser.AvatarUrl);
    Debug.Log("User's identites: " + currentUser.Identities);
});

// You can remove the listener later
GetSocial.RemoveOnCurrentUserChangedListener(listenerId);
1
2
3
4
5
6
GetSocial.addOnCurrentUserChangedListener((currentUser) => {
    print('User is anonymous: $currentUser.isAnonymous'),
    print('User displayName: $currentUser.displayName'),
    print('User avatarURL: $currentUser.avatarUrl'),
    print('User identites: $currentUser.identities'),
});

Next Steps

Well-done! Your user is set up, see what to do next:

Give us your feedback! Was this article helpful?

πŸ˜€ πŸ™