How to pass data from before hook to after hook

Hi,
How can I pass data from before hook to after hook for Authenticate hooks in Lua runtime?
Here’s my test code -

local function before_authenticate_custom(context, payload)
    print("******* before authenticating user -- payload : ")
    print(nk.json_encode(context))
    print("******************")
    print(nk.json_encode(payload))
    print("******************************************")

     payload.user_data = {
        data = "test data"
    }

    return payload
end
nk.register_req_before(before_authenticate_custom, "AuthenticateCustom")

Here are the server logs showing error -

******* before authenticating user -- payload :
{"client_ip":"192.168.1.38","env":{},"execution_mode":"before","query_params":{}}
******************
{"account":{"id":"41a90600df5005c302498cf2231fcf8a33f1c5c9"},"create":true}
******************************************
{"level":"error","ts":"2019-08-14T14:54:33.319+0530","msg":"Could not unmarshall result to request","result":{"account":{"id":"41a90600df5005c302498cf2231fcf8a33f1c5c9"},"create":true,"user_data":{"data":"test data"}},"error":"unknown field \"user_data\" in api.AuthenticateCustomRequest","stacktrace":"github.com/heroiclabs/nakama/server.(*RuntimeProviderLua).BeforeReq\n\t/go/src/github.com/heroiclabs/nakama/server/runtime_lua.go:1206\ngithub.com/heroiclabs/nakama/server.NewRuntimeProviderLua.func3.5\n\t/go/src/github.com/heroiclabs/nakama/server/runtime_lua.go:248\ngithub.com/heroiclabs/nakama/server.(*ApiServer).AuthenticateCustom.func1\n\t/go/src/github.com/heroiclabs/nakama/server/api_authenticate.go:41\ngithub.com/heroiclabs/nakama/server.traceApiBefore\n\t/go/src/github.com/heroiclabs/nakama/server/api.go:509\ngithub.com/heroiclabs/nakama/server.(*ApiServer).AuthenticateCustom\n\t/go/src/github.com/heroiclabs/nakama/server/api_authenticate.go:55\ngithub.com/heroiclabs/nakama/apigrpc._Nakama_AuthenticateCustom_Handler.func1\n\t/go/src/github.com/heroiclabs/nakama/apigrpc/apigrpc.pb.go:1006\ngithub.com/heroiclabs/nakama/server.StartApiServer.func1\n\t/go/src/github.com/heroiclabs/nakama/server/api.go:101\ngithub.com/heroiclabs/nakama/apigrpc._Nakama_AuthenticateCustom_Handler\n\t/go/src/github.com/heroiclabs/nakama/apigrpc/apigrpc.pb.go:1008\ngithub.com/heroiclabs/nakama/vendor/google.golang.org/grpc.(*Server).processUnaryRPC\n\t/go/src/github.com/heroiclabs/nakama/vendor/google.golang.org/grpc/server.go:971\ngithub.com/heroiclabs/nakama/vendor/google.golang.org/grpc.(*Server).handleStream\n\t/go/src/github.com/heroiclabs/nakama/vendor/google.golang.org/grpc/server.go:1250\ngithub.com/heroiclabs/nakama/vendor/google.golang.org/grpc.(*Server).serveStreams.func1.1\n\t/go/src/github.com/heroiclabs/nakama/vendor/google.golang.org/grpc/server.go:690"}

This is a great question. We are adding this in for 2.7.0.

In the meantime, what kind of information are you thinking of passing through from one function to another?

@mofirouz, great news :+1:
So, our use case is, we need to fetch player details from other server and initialize a new user or update already existing user’s information on this game server.
Other server’s player_id is used for custom auth on game server.
If no player data is received from other server in before hook, then we are not letting that user authenticate on the game server.
So, This will require two API calls
First, in the before hook to fetch player details from other server.
And second, in after hook to fetch player details from other server and then update the user’s details in game server upon successful authentication.
All this could be achieved with a single API call in the before hook if we can pass the fetched player data from before hook to after hook.

Any chance that you could, for instance, save the data to a table instance and reference it on the back end? For instance, you have a table that stores player data (table of tables) indexed by the player_id, then in the after hook you look up that table and dump the data you need, then set the entry to nil?

If you’re not going to know the player_id between the two, then it might be session id, or some other connective data. (Alternatively, you could potentially do a table of strings and serialize/unserialize on each side)

Sorry for not being more precise, but I don’t know the extent of the player data.

You can’t do that in the Lua runtime, you should read through the restrictions section of the runtime docs. Global variables can’t be used in the Lua runtime.

It will work if you use the Go runtime, and that’s probably the best solution until we build something into Nakama itself.