Authentication: a real account in one call, an anonymous one in one fewer
Setup & Initialize is what runs right before this; every call below assumes Init() already succeeded. Player Stats & XP picks up immediately after login succeeds.
brainCloud separates a player's Profile from their Identity. The Profile holds everything the player has accumulated: currency, stats, entities, progress. An Identity is just one way of looking that Profile up, and a Profile can carry several: anonymous, email, or one of brainCloud's supported platform identities (Facebook, Google Play, GameCenter, Steam, and more). bitBuddies opens on an anonymous Identity, one call, no email, no password, and the Profile it creates carries forward untouched if the player later attaches a real email Identity to it. Reconnecting an existing session and creating a new one run through the same post-login code path; there's no separate "returning user" branch to maintain.
How it works
- On launch, check for a reconnectable session first. BrainCloudWrapper.CanReconnect() looks for a cached session. If one exists, Wrapper.Reconnect(...) restores it and the login screen is skipped entirely. The client only falls through to login when that check fails.
- An anonymous Identity is one call, no form. AuthenticateAnonymous(success, failure) creates a Profile with nothing typed and nothing validated client-side.
- An email Identity uses the same call, plus one flag. AuthenticateEmailPassword(username, password, createAccount, success, failure). The createAccount flag is the only thing distinguishing a signup from a login; both hit the same endpoint.
- Attaching a second Identity to an existing Profile is a different call. AuthenticateEmailPassword authenticates or creates a Profile from scratch, it does not attach anything. Adding an email Identity to the Profile that's already logged in is IdentityService.AttachEmailIdentity(email, password, success, failure). Switching an already-attached email requires DetachEmailIdentity first.
- One success handler covers every path. Reconnect, anonymous login, and email login all resolve to the same post-login method, and it parses the same response regardless of which path produced it.
Code
public override void Awake()
{
base.Awake();
if (BrainCloudManager.Instance.CanReconnectUser())
{
BrainCloudManager.Instance.ReconnectUser();
StartCoroutine(WaitToReconnect());
}
else
{
SceneLoader.LoadLevel(BitBuddiesConsts.LOGIN_SCENE_NAME);
SceneLoader.ShowLoadingScreen();
}
}private void OnQuickCreateButtonClick()
{
// Authenticate anonymously but still go through the normal logging-in path
_createAccount = true;
IsInteractable = false;
BrainCloudManager.Instance.CurrentUserInfo = new UserInfo();
BrainCloudManager.Wrapper.AuthenticateAnonymous
(
OnSuccess("Anonymous Account creation successfully", OnLoggedInUser),
OnFailure("Anonymous Account creation failed", OnFailureCallback)
);
}private void AuthenticateEmail(string username, string password)
{
BrainCloudManager.Wrapper.AuthenticateEmailPassword
(
username,
password,
_createAccount, // true from "Create Account", false from "Log In"
OnSuccess("Authentication Success", OnLoggedInUser),
OnFailure("", OnFailureCallback)
);
}if (BrainCloudManager.Instance.IsEmailAuthenticated)
{
// Detach then attach new email.
BrainCloudManager.Wrapper.IdentityService.DetachEmailIdentity
(
BrainCloudManager.Instance.CurrentUserInfo.Email,
true,
BrainCloudManager.HandleSuccess("Detach Email Successful", OnDetachEmailSuccess),
BrainCloudManager.HandleFailure("Detach Email Failed", OnAttachEmailFailed)
);
}
else
{
BrainCloudManager.Wrapper.IdentityService.AttachEmailIdentity
(
_attachEmailInputField.text,
_passwordInputField.text,
BrainCloudManager.HandleSuccess("Attach Email Successful", OnAttachEmailSuccess),
BrainCloudManager.HandleFailure("Attach Email Failed", OnAttachEmailFailed)
);
}private void OnLoggedInUser(string jsonResponse)
{
IsInteractable = true;
BrainCloudManager.Instance.OnAuthenticateSuccess(jsonResponse);
StateManager.Instance.GoToParent();
}Gotchas worth knowing up front
- AttachEmailIdentity and AuthenticateEmailPassword do different things. One adds an Identity to the current Profile. The other authenticates or creates a Profile from scratch. Calling the wrong one to preserve a guest's progress creates a second, empty Profile instead of upgrading the existing one.