AuthenticateAnonymous
Authenticate a user anonymously with brainCloud - used for apps that don't want to bother the user to login, or for users who are sensitive to their privacy.
caution
Make sure you've initialized the BrainCloudWrapper before authenticating.
Error Handling Example
public void FailureCallback(int statusCode, int reasonCode, string statusMessage, object cbObject) {
switch (reasonCode) {
case ReasonCodes.MISSING_IDENTITY_ERROR: { // Identity does not match any profile
// Reset Profile ID and re-authenticate
_bc.ResetStoredProfileId();
_bc.AuthenticateAnonymous();
break;
}
case ReasonCodes.SWITCHING_PROFILES: { // Identity belongs to a different profile
// Reset Profile ID and Anonymous id, and then re-authenticate
_bc.ResetStoredProfileId();
_bc.ResetStoredAnonymousId();
_bc.AuthenticateAnonymous();
break;
}
default: { // Uncaught reasonCode // Uncaught reasonCode
// Log the error for debugging later
// ...
break;
}
}
}
Service | Operation |
---|---|
authenticationV2 | AUTHENTICATE |
Usage
http://localhost:3000
- C#
- C++
- Objective-C
- Java
- JavaScript
- Dart
- Cloud Code
- Raw
SuccessCallback successCallback = (response, cbObject) =>
{
Debug.Log(string.Format("[Authenticate Success] {0}", response));
};
FailureCallback failureCallback = (status, code, error, cbObject) =>
{
Debug.Log(string.Format("[Authenticate Failed] {0} {1} {2}", status, code, error));
};
_bc.AuthenticateAnonymous(successCallback, failureCallback);
_bc->authenticateAnonymous(this);
[_bc authenticateAnonymous:successBlock
errorCompletionBlock:failureBlock
cbObject:nil];
_bc.authenticateAnonymous();
_bc.authenticateAnonymous(result =>
{
var status = result.status;
console.log(status + " : " + JSON.stringify(result, null, 2));
});
ServerResponse result = await _bc.authenticateAnonymous();
if (result.statusCode == 200) {
print("Success");
} else {
print("Failed ${result.error['status_message'] ?? result.error}");
}
_bc.authenticateAnonymous(result =>
{
var status = result.status;
console.log(status + " : " + JSON.stringify(result, null, 2));
});
_bc.authenticateAnonymous(result =>
{
var status = result.status;
console.log(status + " : " + JSON.stringify(result, null, 2));
});
JSON Response
{
"data": {
"abTestingId": 95,
"lastLogin": 1713973000159,
"server_time": 1713973000235,
"refundCount": 0,
"timeZoneOffset": -5.0,
"experiencePoints": 0,
"maxBundleMsgs": 10,
"createdAt": 1713973000153,
"parentProfileId": null,
"emailAddress": null,
"experienceLevel": 1,
"countryCode": null,
"vcClaimed": 0,
"currency": {
"bar": {
"consumed": 0,
"balance": 0,
"purchased": 0,
"awarded": 0,
"revoked": 0
},
"coins": {
"consumed": 0,
"balance": 8,
"purchased": 0,
"awarded": 8,
"revoked": 0
}
},
"id": "15e5ce33-2411-45f8-a29e-7f600880113a",
"compressIfLarger": 51200,
"amountSpent": 0,
"previousLogin": null,
"playerName": "",
"pictureUrl": null,
"incoming_events": [],
"sessionId": "gbgakmm4hmt15e2pobvmh7ptck",
"languageCode": "en",
"vcPurchased": 0,
"isTester": false,
"summaryFriendData": null,
"loginCount": 1,
"emailVerified": true,
"xpCapped": false,
"profileId": "15e5ce33-2411-45f8-a29e-7f600880113a",
"newUser": "true",
"playerSessionExpiry": 1200,
"sent_events": [],
"maxKillCount": 11,
"rewards": {
"rewardDetails": {
"xp": {
"experienceLevels": [
{
"level": 1,
"rewards": {
"currency": {
"coins": 8
}
}
}
]
}
},
"currency": {
"bar": {
"consumed": 0,
"balance": 0,
"purchased": 0,
"awarded": 0,
"revoked": 0
},
"coins": {
"consumed": 0,
"balance": 8,
"purchased": 0,
"awarded": 8,
"revoked": 0
}
},
"rewards": {}
},
"statistics": {
"test": 0.99,
"HITLEVELNVEHICLE_000005": 0
}
},
"status": 200
}
Common Error Code
Status Codes
Code | Name | Description |
---|---|---|
40206 | MISSING_IDENTITY_ERROR | Both an anonymousId and profileId were provided for authentication - but the anonymousId doesn't exist on the server. The profileId may or may not exist. It is possible that the user account was deleted via the Design Portal. The proper recourse is to reset the stored profile id, and re-authenticate. [There is no need to delete the anonymousId since it doesn't exist on the server anyway.] |
40207 | SWITCHING_PROFILES | This means that the anonymousId provided does point to a profile, but not the same one that was saved in the client. This fails the anonymous security check. For any other authentication type, this might indicate that the user wants to switch accounts (thus the name of the error constant). For anonymous, the only response is to reset both the stored anonymousId and profileId, and then re-authenticate. |
40217 | UNKNOWN_AUTH_ERROR | An unknown error has occurred during authentication |