Get or ban user by custom id

Hi,

We implemented a custom authentication flow using the custom id feature in one of our social chatting apps from the frontend. Our server backend does not have any idea regarding Nakama and we would like to keep it that way.

Now for our admins, we would like to deploy a feature where admins will be able to ban users from Nakama Console. Here, our admins only have the user id from the backend.

Currently, Nakama does not support fetching user info via custom id which I needed to call user ban API.
Should we write an RPC method to execute the query to find the Nakama user id since you already indexed the custom_id

  1. Nakama: Nakama: 3.8.0
  2. Server Framework Runtime language: Lua

Hi Ahamed, why do you want to avoid any knowledge of Nakama user ids in your own backend system? The cleanest solution would be to store the Nakama user id in your own backend’s database and pass that to a ban RPC. Otherwise, yes writing a custom RPC and SQL query to delete an account with the provided custom_id would be sufficient for this.

This is our design decision. We are developing the Nakama + Unity platform as a plugin and the backend I am talking about can be any system.

So for now we are going with this for now.

local nk = require("nakama")
local res = require("response_helper") // This is for formatting the response


local function search_by_custom_id(custom_id)
    local query = [[
        SELECT *
        FROM users
        WHERE custom_id = $1
        LIMIT 100
    ]]

    local rows = nk.sql_query(query, { custom_id })

    if #rows == 0 then
        return nil
    end

    return rows
end


function user.ban_by_custom_id(context, payload)
    local data = nk.json_decode(payload)
    nk.logger_info("Users using custom id " .. data.custom_id .. " is being banned from ip " .. context.client_ip)

    local users = search_by_custom_id(data.custom_id)
    if users == nil or #users == 0 then
        nk.logger_info("No user found by custom_id " .. data.custom_id)
        return nk.json_encode(res.get_response(SuccessCode.NotFound, string.format("No user found"), nil))
    end

    nk.logger_info("Total user found by custom_id " .. data.custom_id .. " : " .. #users)

    local user_ids = {}
    for _, u in ipairs(users) do
        nk.logger_warn("Username " .. u.username .. " is being banned, custom_id: " .. u.id)
        table.insert(user_ids, u.id)
    end

    nk.users_ban_id(user_ids)
    nk.logger_warn("Total " .. #user_ids .. " user banned")

    return nk.json_encode(res.get_response(SuccessCode.Ok, "Total " .. #user_ids .. " user banned", user_ids))
end