Server side API to transfer a login identity between existing users

Hello!

I’m working on account linking and I find myself needing a method like this in the nakama api: AccountTransferIdentity. This method would be only callable on the server.

Context: my game does a silent authentication flow: on startup, it authenticates using the platform provider (currently steam), falling back to a local device_id if platform auth is not available or fails. A nakama user is created if necessary. After authentication with a platform, I use the device_id and attempt to link it to the authenticated account, whether that account is new or existing

What I would like: Some sort of go API to transfer a login identity from UserA to UserB. Example:
UserA has 3 login identities: Steam, Google, and device_id
UserB has 3 login identities: Apple, Nintendo, and device_id

I want the server (in this case, a custom nakama go RPC) to call something like nk.AccountTransferIdentity(ctx, userAID, userBID, "google", googleID) to atomically move UserA’s Google identity to UserB without requiring a fresh Google token, leaving all other linked identities unchanged. You could also return useful errors here for if UserB already has a google identity, etc

Why?: I am implementing cross platform account linking where both Nakama accounts already exist. The flow is like this:

  1. Player on DeviceA starts account linking process, receives a 6 digit code from the server
  2. Player goes to DeviceB, enters the 6 digit code, receives another 6 digit confirmation code from server
  3. Player goes to DeviceA and enters that confirmation code
  4. Account merging UX appears and user confirms
  5. All login identities are transferred individually on the server and the old user is deleted

I like this because it is entirely frictionless, the user never has to worry about going to his other devices (iPhone, android, Nintendo) and link them individually. The server can validate “these 2 users have proved to be the same, I will manually transfer each identity”.

Thanks for reading and let me know if this is something the Heroic Labs team would be open to. If so I can submit a draft PR if it makes things easier

1 Like

Hi @ctrhinkle,

Whilst I see why you’d want a dedicated function, I think that this can easily be achieved with the current runtime functions and some custom SQL.

The main issue here is that we have database constraints that disallow 2 accounts to share the same provider ID, so you cannot migrate say, UserA->UserB without deleting UserA first, otherwise you’d get a unique constraint error when trying to Link (or run SQL) updating the target account external ids.

I’d do the following (assuming the target is UserB):

  1. Read UserA external identifiers via GetAccount (google_id and others if needed).
  2. Delete UserA account with the DeleteAccount runtime function.
  3. Use a bit of custom sql to update UserB account, skipping token validation:
UPDATE users SET google_id = coalesce(google_id, $1) WHERE id = $2

Coalesce is just a safeguard to not overwrite an existing google_id in the target account but you can tweak the query to suit your needs.

If you’d like to add an additional safeguard, instead of 2), you could run some custom SQLl to remove the current linked external IDs:

UPDATE users SET google_id = NULL, device_id = NULL, steam_id = NULL WHERE id = $2

This will keep the account around, but “unlink” it from all the providers in the query, allowing the update in 3. I’d also store some metadata in the new account, or a dedicated storage object(s) under the target user_id, so you can lookup the orphaned account, if needed.

Hope this helps

1 Like

Hi @sesposito

Thanks so much for your quick response! These are genuinely helpful tips and I’ll definitely use these if no api changes are planned

I was thinking of doing it with custom SQL but I usually try to avoid these kinds of things so that unexpexted migrations don’t affect my query. In this case, it seems extremely unlikely to change but it’d be something I’d have to document for our team

The issue you mentioned of having the constraint to not have 2 identities on the same user at once is true. I was thinking the api could implement a transaction to handle the deletion first, assign it, then rollback in the case of a failure, but it is inherently messy, complex, and prone to failure so I see why you wouldn’t want to do this

Another proposal I have is maybe instead of having atransfer identity method, there could just be a method to manually add the identity id for a specific provider without a token? This would avoid custom SQL altogether so any future db migrations don’t silently break for users and would be a simpler implementation. It would also have more clear and uunderstandable error states. How does this sound?

I’d feel more comfortable avoiding custom SQL but if it’s necessary and you’re recommending it I’ll probably go that route. Thanks again for all your time and help!