Before/while authenticating, disconnect old connection if avaliable

What is the recommended approach to prevent the same user from authenticating on multiple devices?

What I would like to do is, when a user tries to connect to Nakama, I would like to disable the reconnect logic and disconnect him from all the connected devices with a meaningful message, for example:

A new device is connected using your account, logging out from this device

You have a configuration labeled session.single_socket, but this won’t serve my purpose since the client won’t get the reason why the user is suddenly disconnected from this device.

If possible please, let me know the possible Nakama Lua runtime to be used to solve this issue.

I’ve added a register_req_after hook, but I am not sure if it’s a reliable way to achieve the goal.

function after_match_join(context, payload)
    local count = nk.stream_count({mode = 0, subject = context.user_id})
    if (count >= 2) then
        nk.logger_info("User is trying to join a match from another device: ".. context.user_id)
        nk.notification_send(context.user_id, "A new user trying to join a match to this account", {}, 4)
    end
end
  1. Nakama: Nakama: 3.8.0
  2. Server Framework Runtime language: Lua
  3. Unity SDK: 2.8.1

Hello @ahamed, please have a look at this thread: Preventing users from having multiple sessions - #6 by novabyte, it should give you what you need.

Best.

Thanks, @sesposito for the quick reply, already did a similar thing. Now would like to end all active sessions of that user from the server. Is there any API for that?

You should use