How to pass data from before hook to after hook

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 : ")

     payload.user_data = {
        data = "test data"

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

Here are the server logs showing error -

******* before authenticating user -- payload :
{"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":"*RuntimeProviderLua).BeforeReq\n\t/go/src/\\n\t/go/src/\*ApiServer).AuthenticateCustom.func1\n\t/go/src/\\n\t/go/src/\*ApiServer).AuthenticateCustom\n\t/go/src/\\n\t/go/src/\\n\t/go/src/\\n\t/go/src/\*Server).processUnaryRPC\n\t/go/src/\*Server).handleStream\n\t/go/src/\*Server).serveStreams.func1.1\n\t/go/src/"}

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.