How to make user disconnect at RegisterBeforeAuthenticateXXXX

Hello everyone, I have a problem, If i want to kick user at “RegisterBeforeAuthenticateEmail” this checkpoint, How to make user disconnect?

I assume by “kick” you are actually referring to disallowing the user to authenticate with the system.

According to the documentation from this page:

If you choose to return nil instead of the payload (or a non-nil error in Go) the server will halt further processing of that message. This can be a used to stop the server from accepting certain messages or disabling/blacklisting certain server features.

This is quite simple and can be done with a few lines of Lua/Go:

local nk = require("nakama")

local M = {}

function M.authenticate_email(context, payload)
  local email = payload.account.email
  -- local password = payload.account.password

  -- perform custom logic here
  if (email == "pineapple@pizza.com") then
    return nil
  end

  -- make sure that you return payload
  return payload
end
nk.register_req_before(M.authenticate_email, "AuthenticateEmail")

return M

thanks your answer.

Hi @mofirouz, May I ask you more questions about this case?
The thing is I can disconnect use by return nil, errors.New(“error”) in the server. However, the user client can’t detect this message. I mean I can’t catch any exception in the client, it just awaits forever.
Here is my unity client code

try
						{
							session = await client.AuthenticateDeviceAsync(deviceId, deviceId);
							PlayerPrefs.SetString(RefreshTokenKey, session.RefreshToken);
						}
						catch (ApiResponseException ex)
						{
							Debug.LogFormat("Error: {0}", ex.Message);
						}
						catch (Exception e2)
						{
							Debug.LogError(e2.ToString());
						}

Ok firstly, the word “disconnect” is misleading - you are not connected to a socket to be disconnected from the said socket.

Secondly, please post your Go code, as I suspect you have an issue there; have a look at this for guidance:

Here is my go code:

func BeforeAuthenticateDevice(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateDeviceRequest) (*api.AuthenticateDeviceRequest, error) {
	 logger.warn("beforeauthenticatedevice:" + in.account.id + " " + in.username + " deviceid: " + in.account.id)
	 if len(in.account.id) != 32 || strings.index(in.account.id, "somefixedkey") != 0 {
	 	logger.warn("cannot authenticate your token")
	 	return nil, errors.new("cannot authenticate your token")
	 }
	return in, nil
}

You need to return a particular type of error in your Go code that follows this:

Your code should be:

func BeforeAuthenticateDevice(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateDeviceRequest) (*api.AuthenticateDeviceRequest, error) {

//....

return nil, nk.NewError("cannot authenticate your token", 13) // Internal Code = 13, which returns a HTTP 500.
}

I’m sorry because nk has no NewError function but the runtime does.
So, I tried as follows:
return nil, runtime.NewError("Invalid token, please login again", 13)
but the client still cannot catch the exception

How are you registering your BeforeHook? Can you show me that too plz?

Thank you for your reply,
Of course, I registered and I also check it by putting some logs to the AuthenticateDevice function too.

initializer.RegisterBeforeAuthenticateDevice(BeforeAuthenticateDevice)