-
Notifications
You must be signed in to change notification settings - Fork 9
User Login and Management
Depending upon which vendor you use as the main back-end for game services (currently only Xtralife is supported), different types of user authentication and management will be available. As this service module is very important and Facebook Instant Games SDK does not provide any extra user management functionality, a lot of additional functionality has been added via FBinstant.ext to make the connection between your game and its users more meaningful. Lets begin by looking at user authentication and login
By default the default Xtralife UserService back-end will automatically create an anonymous user account for users that have just started to play your game. This is fine for most purposes and if you are happy with this set up then you do not need to do any more. However, because the users login data is persisted in the browser when the user plays your game from a different device they will be given a new user account and will lose all of their game progress.
Out of the box, IGX will log all users in anonymously. If you would like to take over the login process yourself then you can disable auto anonymous login by setting FBInstant.options.userOptions.allowAnonymous to false
Lets take a look at how to log a user in via email and password:
if (FBInstant.ext !== undefined)
{
FBInstant.ext.loginWithEmailAsync(email, password, options).then(function(error, gamer) {
if (error === null)
// Logged in successfully
});
}
Before you can login to Facebook you must firstly initialise Facebook:
LibSocial.Facebook.Init("facebook_app_id", function(response) {
})
You will need to create an app on Facebook in order to obtain a Facebook App ID.
Lets take a look at how to log a user in via Facebook:
if (FBInstant.ext !== undefined)
{
LibSocial.Facebook.Login(function(response) {
FBInstant.ext.loginWithFacebookAccessTokenAsync(response.authResponse.accessToken).then(function(error, gamer)
{
if (error === null)
// Logged in with Facebook
});
});
}
Once the user is connected and logged in via Facebook, their name, email address and Facebook profile picture url will be stored in the users profile.
The user can log in via a short code which they can retrieve in cases where they forget their account details.
Users often forget log in details, so the user can request a short code to log in with to restore their session.
if (FBInstant.ext !== undefined)
{
FBInstant.ext.resetPasswordAsync("[email protected]", "[email protected]", "Lost Password", { body: "You can login with this <b>[[SHORTCODE]]</b>", html: true }).then(function(error) {
if (error === null)
// Success
});
}
Users sometimes want to change their password, the example below shows how to change the logged in users password:
if (FBInstant.ext !== undefined)
{
FBInstant.ext.changePasswordAsync("mynewpassword").then(function(error) {
if (error === null)
// Success
});
}
Once the user is logged in anonymously, it is possible to link their account to an email account or social account such as Facebook. Lets take a look at an example:
LibSocial.Facebook.Login(function(response) {
FBInstant.ext.convertAccountAsync("facebook", response.authResponse.userID, response.authResponse.accessToken, function(error) {
if (error ==== null)
// success
});
});
});
Once the account is updated the users social profile will be available.
The user can be logged out as follows:
FBInstant.ext.logoutAsync(function(error) {
if (error ==== null)
// success
});
To get the users profile data you can use the same process as IG:
var name = FBInstant.player.getName();
var photo = FBInstant.player.getPhoto();
var id = FBInstant.player.getID();
You can update individual fields of the users profile once the are logged in. Lets take a look at how to update the users display name:
FBInstant.ext.setProfileAsync({displayName: "new_name"}).then(function() {
});
This offers an easy way for anonymous users that do not connect via a social account to be able to customise their game.
You can check the users login status as follows:
if (FBInstant.ext.isLoggedIn())
{
// User is logged in
}
You can also get the type of login that the user has logged in with:
var login_type = FBInstant.ext.getLoginType();
You can determine when the user created their account:
var account_date = FBInstant.ext.getRegistrationDate();
You can determine which games the logged in player is playing as follows:
var games = FBInstant.ext.getGames(); // Returns an array of UserService.Game
