Initialize a New User

It’s often useful when a new user registers to have a bunch of records setup for them. In games this could be needed for a user’s virtual wallet, initial inventory items, etc. In this tutorial we’ll cover a few different ways to handle this use case.

After register callback

The simplest approach is to write records in the success callback for the register function in a client.

This code demonstrates how to do it with a condensed example. In real application code you’ll break up the authentication and connect logic from the storage writes based on how you manage connect and reconnect.

var deviceId = SystemInfo.deviceUniqueIdentifier;
var session = await client.AuthenticateDeviceAsync(deviceId);

var json = "{\"coins\": 100, \"gems\": 10, \"artifacts\": 0}";
var object = new WriteStorageObject = {
  "collection" = "wallets",
  "key" = "mywallet",
  "value" = json
};
const storageWriteAck = await client.WriteStorageObjectsAsync(session, objects);
Debug.Log("Successfully setup new user's records.");

This code has trade-offs which should be noted. A disconnect can happen before the records are written to storage. This may leave the setup of the user incomplete and the application in a bad state.

This option is only worth choosing when you want to avoid writing server-side code or have built retry logic on top of a client.

Server-side hook

Another way to write records for the new user is to run server-side code after registration has completed. This can be done with a register hook.

The “register_after” hook can be used with one of the "authenticaterequest_*" message types to tell the server to run a function after that message has been processed. It’s important to note that the server does not distinguish between register and login messages so we use a conditional write to store the records.

Code snippet for this language TypeScript has not been found. Please choose another language to show equivalent examples.

This approach avoids the trade-off with client disconnects but requires a database write to happen after every login or register message. This could be acceptable d

Last updated