Guarding APIs #

There are many reasons why you may want to limit or completely prevent client access to any or all of the Nakama APIs, such as performing any desired validation on a client request or preventing client attempts at circumventing your game logic.

Nakama provides multiple avenues for you to secure your game against malicious client actions:

  • Implementing game logic on the server as RPCs or hooks
  • Setting access controls on storage objects. Objects created via the REST API will always be owned by the authenticated user and you can set if other players have read access to this data by setting the read permissions accordingly.
  • Server-to-server calls can be secured via HTTP key.
  • Use before hooks to disable any APIs you don’t want to be directly accessible by clients.

Using before hooks #

To disable any API, register a before hook on the function call you want to disable and instead of returning the request input, return nil. Nakama will automatically handle any such scenario as a disabled API call.

In the example implementation provided below, any message names and API calls that are commented out remain available to clients while all others are disabled:

Server
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package main

import (
	"context"
	"database/sql"

	"github.com/heroiclabs/nakama-common/api"
	"github.com/heroiclabs/nakama-common/rtapi"
	"github.com/heroiclabs/nakama-common/runtime"
)

func registerGuard(initializer runtime.Initializer) error {
	rtMessages := []string{
		"ChannelJoin",
		"ChannelLeave",
		"ChannelMessageSend",
		"ChannelMessageUpdate",
		"ChannelMessageRemove",
		"MatchCreate",
		"MatchDataSend",
		"MatchJoin",
		"MatchLeave",
		"MatchmakerAdd",
		"MatchmakerRemove",
		"PartyCreate",
		"PartyCreate",
		"PartyJoin",
		"PartyLeave",
		"PartyPromote",
		"PartyAccept",
		"PartyRemove",
		"PartyClose",
		"PartyJoinRequestList",
		"PartyMatchmakerAdd",
		"PartyMatchmakerRemove",
		"PartyDataSend",
		//"Ping",
		//"Pong",
		"Rpc",
		"StatusFollow",
		"StatusUnfollow",
		"StatusUpdate",
	}
	for _, rtMessage := range rtMessages {
		if err := initializer.RegisterBeforeRt(rtMessage, func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, envelope *rtapi.Envelope) (*rtapi.Envelope, error) {
			return nil, nil
		}); err != nil {
			return err
		}
	}

	if err := initializer.RegisterBeforeAddFriends(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AddFriendsRequest) (*api.AddFriendsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAddGroupUsers(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AddGroupUsersRequest) (*api.AddGroupUsersRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateApple(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateAppleRequest) (*api.AuthenticateAppleRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateCustom(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateCustomRequest) (*api.AuthenticateCustomRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateDevice(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateDeviceRequest) (*api.AuthenticateDeviceRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateEmail(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateEmailRequest) (*api.AuthenticateEmailRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateFacebook(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateFacebookRequest) (*api.AuthenticateFacebookRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateFacebookInstantGame(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateFacebookInstantGameRequest) (*api.AuthenticateFacebookInstantGameRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateGameCenter(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateGameCenterRequest) (*api.AuthenticateGameCenterRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateGoogle(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateGoogleRequest) (*api.AuthenticateGoogleRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeAuthenticateSteam(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateSteamRequest) (*api.AuthenticateSteamRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeBanGroupUsers(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.BanGroupUsersRequest) (*api.BanGroupUsersRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeBlockFriends(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.BlockFriendsRequest) (*api.BlockFriendsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeCreateGroup(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.CreateGroupRequest) (*api.CreateGroupRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	//if err := initializer.RegisterBeforeDeleteAccount(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule) error {
	//	return nil
	//}); err != nil {
	//	return err
	//}
	if err := initializer.RegisterBeforeDeleteFriends(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.DeleteFriendsRequest) (*api.DeleteFriendsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeDeleteGroup(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.DeleteGroupRequest) (*api.DeleteGroupRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeDeleteLeaderboardRecord(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.DeleteLeaderboardRecordRequest) (*api.DeleteLeaderboardRecordRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeDeleteNotifications(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.DeleteNotificationsRequest) (*api.DeleteNotificationsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeDeleteStorageObjects(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.DeleteStorageObjectsRequest) (*api.DeleteStorageObjectsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeDeleteTournamentRecord(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.DeleteTournamentRecordRequest) (*api.DeleteTournamentRecordRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeDemoteGroupUsers(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.DemoteGroupUsersRequest) (*api.DemoteGroupUsersRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	//if err := initializer.RegisterBeforeEvent(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.Event) (*api.Event, error) {
	//	return nil, nil
	//}); err != nil {
	//	return err
	//}
	//if err := initializer.RegisterBeforeGetAccount(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule) error {
	//	return nil
	//}); err != nil {
	//	return err
	//}
	if err := initializer.RegisterBeforeGetSubscription(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.GetSubscriptionRequest) (*api.GetSubscriptionRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeGetUsers(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.GetUsersRequest) (*api.GetUsersRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeImportFacebookFriends(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ImportFacebookFriendsRequest) (*api.ImportFacebookFriendsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeImportSteamFriends(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ImportSteamFriendsRequest) (*api.ImportSteamFriendsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeJoinGroup(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.JoinGroupRequest) (*api.JoinGroupRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeJoinTournament(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.JoinTournamentRequest) (*api.JoinTournamentRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeKickGroupUsers(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.KickGroupUsersRequest) (*api.KickGroupUsersRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLeaveGroup(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.LeaveGroupRequest) (*api.LeaveGroupRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkApple(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountApple) (*api.AccountApple, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkCustom(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountCustom) (*api.AccountCustom, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkDevice(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountDevice) (*api.AccountDevice, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkEmail(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountEmail) (*api.AccountEmail, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkFacebook(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.LinkFacebookRequest) (*api.LinkFacebookRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkFacebookInstantGame(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountFacebookInstantGame) (*api.AccountFacebookInstantGame, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkGameCenter(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountGameCenter) (*api.AccountGameCenter, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkGoogle(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountGoogle) (*api.AccountGoogle, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeLinkSteam(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.LinkSteamRequest) (*api.LinkSteamRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListChannelMessages(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListChannelMessagesRequest) (*api.ListChannelMessagesRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListFriends(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListFriendsRequest) (*api.ListFriendsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListGroupUsers(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListGroupUsersRequest) (*api.ListGroupUsersRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListGroups(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListGroupsRequest) (*api.ListGroupsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListLeaderboardRecords(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListLeaderboardRecordsRequest) (*api.ListLeaderboardRecordsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListLeaderboardRecordsAroundOwner(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListLeaderboardRecordsAroundOwnerRequest) (*api.ListLeaderboardRecordsAroundOwnerRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListMatches(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListMatchesRequest) (*api.ListMatchesRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListNotifications(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListNotificationsRequest) (*api.ListNotificationsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListStorageObjects(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListStorageObjectsRequest) (*api.ListStorageObjectsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListSubscriptions(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListSubscriptionsRequest) (*api.ListSubscriptionsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListTournamentRecords(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListTournamentRecordsRequest) (*api.ListTournamentRecordsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListTournamentRecordsAroundOwner(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListTournamentRecordsAroundOwnerRequest) (*api.ListTournamentRecordsAroundOwnerRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListTournaments(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListTournamentsRequest) (*api.ListTournamentsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeListUserGroups(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ListUserGroupsRequest) (*api.ListUserGroupsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforePromoteGroupUsers(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.PromoteGroupUsersRequest) (*api.PromoteGroupUsersRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeReadStorageObjects(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ReadStorageObjectsRequest) (*api.ReadStorageObjectsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	//if err := initializer.RegisterBeforeSessionLogout(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.SessionLogoutRequest) (*api.SessionLogoutRequest, error) {
	//	return nil, nil
	//}); err != nil {
	//	return err
	//}
	//if err := initializer.RegisterBeforeSessionRefresh(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.SessionRefreshRequest) (*api.SessionRefreshRequest, error) {
	//	return nil, nil
	//}); err != nil {
	//	return err
	//}
	if err := initializer.RegisterBeforeUnlinkApple(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountApple) (*api.AccountApple, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUnlinkCustom(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountCustom) (*api.AccountCustom, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUnlinkDevice(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountDevice) (*api.AccountDevice, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUnlinkEmail(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountEmail) (*api.AccountEmail, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUnlinkFacebook(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountFacebook) (*api.AccountFacebook, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUnlinkFacebookInstantGame(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountFacebookInstantGame) (*api.AccountFacebookInstantGame, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUnlinkGameCenter(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountGameCenter) (*api.AccountGameCenter, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUnlinkGoogle(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountGoogle) (*api.AccountGoogle, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUnlinkSteam(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AccountSteam) (*api.AccountSteam, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUpdateAccount(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.UpdateAccountRequest) (*api.UpdateAccountRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeUpdateGroup(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.UpdateGroupRequest) (*api.UpdateGroupRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeValidatePurchaseApple(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ValidatePurchaseAppleRequest) (*api.ValidatePurchaseAppleRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeValidatePurchaseFacebookInstant(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ValidatePurchaseFacebookInstantRequest) (*api.ValidatePurchaseFacebookInstantRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeValidatePurchaseGoogle(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ValidatePurchaseGoogleRequest) (*api.ValidatePurchaseGoogleRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeValidatePurchaseHuawei(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ValidatePurchaseHuaweiRequest) (*api.ValidatePurchaseHuaweiRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeValidateSubscriptionApple(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ValidateSubscriptionAppleRequest) (*api.ValidateSubscriptionAppleRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeValidateSubscriptionGoogle(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.ValidateSubscriptionGoogleRequest) (*api.ValidateSubscriptionGoogleRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeWriteLeaderboardRecord(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.WriteLeaderboardRecordRequest) (*api.WriteLeaderboardRecordRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeWriteStorageObjects(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.WriteStorageObjectsRequest) (*api.WriteStorageObjectsRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}
	if err := initializer.RegisterBeforeWriteTournamentRecord(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.WriteTournamentRecordRequest) (*api.WriteTournamentRecordRequest, error) {
		return nil, nil
	}); err != nil {
		return err
	}

	return nil
}
Server
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
let InitModule: nkruntime.InitModule = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
  initializer.registerRtBefore("ChannelJoin", guardFunction);
  initializer.registerRtBefore("ChannelLeave", guardFunction);
  initializer.registerRtBefore("ChannelMessageSend", guardFunction);
  initializer.registerRtBefore("ChannelMessageUpdate", guardFunction);
  initializer.registerRtBefore("ChannelMessageRemove", guardFunction);
  initializer.registerRtBefore("MatchCreate", guardFunction);
  initializer.registerRtBefore("MatchDataSend", guardFunction);
  initializer.registerRtBefore("MatchJoin", guardFunction);
  initializer.registerRtBefore("MatchLeave", guardFunction);
  initializer.registerRtBefore("MatchmakerAdd", guardFunction);
  initializer.registerRtBefore("MatchmakerRemove", guardFunction);
  initializer.registerRtBefore("PartyCreate", guardFunction);
  initializer.registerRtBefore("PartyJoin", guardFunction);
  initializer.registerRtBefore("PartyLeave", guardFunction);
  initializer.registerRtBefore("PartyPromote", guardFunction);
  initializer.registerRtBefore("PartyAccept", guardFunction);
  initializer.registerRtBefore("PartyRemove", guardFunction);
  initializer.registerRtBefore("PartyClose", guardFunction);
  initializer.registerRtBefore("PartyJoinRequestList", guardFunction);
  initializer.registerRtBefore("PartyMatchmakerAdd", guardFunction);
  initializer.registerRtBefore("PartyMatchmakerRemove", guardFunction);
  initializer.registerRtBefore("PartyDataSend", guardFunction);
  // initializer.registerRtBefore("Ping", guardFunction);
  // initializer.registerRtBefore("Pong", guardFunction);
  initializer.registerRtBefore("Rpc", guardFunction);
  initializer.registerRtBefore("StatusFollow", guardFunction);
  initializer.registerRtBefore("StatusUnfollow", guardFunction);
  initializer.registerRtBefore("StatusUpdate", guardFunction);
  initializer.registerBeforeRt(guardFunction);
  initializer.registerBeforeAddFriends(guardFunction);
  initializer.registerBeforeAddGroupUsers(guardFunction);
  initializer.registerBeforeAuthenticateApple(guardFunction);
  initializer.registerBeforeAuthenticateCustom(guardFunction);
  initializer.registerBeforeAuthenticateDevice(guardFunction);
  initializer.registerBeforeAuthenticateEmail(guardFunction);
  initializer.registerBeforeAuthenticateFacebook(guardFunction);
  initializer.registerBeforeAuthenticateFacebookInstantGame(guardFunction);
  initializer.registerBeforeAuthenticateGameCenter(guardFunction);
  initializer.registerBeforeAuthenticateGoogle(guardFunction);
  initializer.registerBeforeAuthenticateSteam(guardFunction);
  initializer.registerBeforeBanGroupUsers(guardFunction);
  initializer.registerBeforeBlockFriends(guardFunction);
  initializer.registerBeforeCreateGroup(guardFunction);
  // initializer.registerBeforeDeleteAccount(guardFunction);
  initializer.registerBeforeDeleteFriends(guardFunction);
  initializer.registerBeforeDeleteGroup(guardFunction);
  initializer.registerBeforeDeleteLeaderboardRecord(guardFunction);
  initializer.registerBeforeDeleteNotifications(guardFunction);
  initializer.registerBeforeDeleteStorageObjects(guardFunction);
  initializer.registerBeforeDeleteTournamentRecord(guardFunction);
  initializer.registerBeforeDemoteGroupUsers(guardFunction);
  // initializer.registerBeforeEvent(guardFunction);
  // initializer.registerBeforeGetAccount(guardFunction);
  initializer.registerBeforeGetSubscription(guardFunction);
  initializer.registerBeforeGetUsers(guardFunction);
  initializer.registerBeforeImportFacebookFriends(guardFunction);
  initializer.registerBeforeImportSteamFriends(guardFunction);
  initializer.registerBeforeJoinGroup(guardFunction);
  initializer.registerBeforeJoinTournament(guardFunction);
  initializer.registerBeforeKickGroupUsers(guardFunction);
  initializer.registerBeforeLeaveGroup(guardFunction);
  initializer.registerBeforeLinkApple(guardFunction);
  initializer.registerBeforeLinkCustom(guardFunction);
  initializer.registerBeforeLinkDevice(guardFunction);
  initializer.registerBeforeLinkEmail(guardFunction);
  initializer.registerBeforeLinkFacebook(guardFunction);
  initializer.registerBeforeLinkFacebookInstantGame(guardFunction);
  initializer.registerBeforeLinkGameCenter(guardFunction);
  initializer.registerBeforeLinkGoogle(guardFunction);
  initializer.registerBeforeLinkSteam(guardFunction);
  initializer.registerBeforeListChannelMessages(guardFunction);
  initializer.registerBeforeListFriends(guardFunction);
  initializer.registerBeforeListGroupUsers(guardFunction);
  initializer.registerBeforeListGroups(guardFunction);
  initializer.registerBeforeListLeaderboardRecords(guardFunction);
  initializer.registerBeforeListLeaderboardRecordsAroundOwner(guardFunction);
  initializer.registerBeforeListMatches(guardFunction);
  initializer.registerBeforeListNotifications(guardFunction);
  initializer.registerBeforeListStorageObjects(guardFunction);
  initializer.registerBeforeListSubscriptions(guardFunction);
  initializer.registerBeforeListTournamentRecords(guardFunction);
  initializer.registerBeforeListTournamentRecordsAroundOwner(guardFunction);
  initializer.registerBeforeListTournaments(guardFunction);
  initializer.registerBeforeListUserGroups(guardFunction);
  initializer.registerBeforePromoteGroupUsers(guardFunction);
  initializer.registerBeforeReadStorageObjects(guardFunction);
  // initializer.registerBeforeSessionLogout(guardFunction);
  // initializer.registerBeforeSessionRefresh(guardFunction);
  initializer.registerBeforeUnlinkApple(guardFunction);
  initializer.registerBeforeUnlinkCustom(guardFunction);
  initializer.registerBeforeUnlinkDevice(guardFunction);
  initializer.registerBeforeUnlinkEmail(guardFunction);
  initializer.registerBeforeUnlinkFacebook(guardFunction);
  initializer.registerBeforeUnlinkFacebookInstantGame(guardFunction);
  initializer.registerBeforeUnlinkGameCenter(guardFunction);
  initializer.registerBeforeUnlinkGoogle(guardFunction);
  initializer.registerBeforeUnlinkSteam(guardFunction);
  initializer.registerBeforeUpdateAccount(guardFunction);
  initializer.registerBeforeUpdateGroup(guardFunction);
  initializer.registerBeforeValidatePurchaseApple(guardFunction);
  initializer.registerBeforeValidatePurchaseFacebookInstant(guardFunction);
  initializer.registerBeforeValidatePurchaseGoogle(guardFunction);
  initializer.registerBeforeValidatePurchaseHuawei(guardFunction);
  initializer.registerBeforeValidateSubscriptionApple(guardFunction);
  initializer.registerBeforeValidateSubscriptionGoogle(guardFunction);
  initializer.registerBeforeWriteLeaderboardRecord(guardFunction);
  initializer.registerBeforeWriteStorageObjects(guardFunction);
  initializer.registerBeforeWriteTournamentRecord(guardFunction);  
}

const guardFunction = function() {
  return null;
};

// Reference InitModule to avoid it getting removed on build
!InitModule && InitModule.bind(null);
Server
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local nk = require("nakama")

local function guard_function(context, payload)
	return nil
end

nk.register_rt_before(guard_function, "ChannelJoin")
nk.register_rt_before(guard_function, "ChannelLeave")
nk.register_rt_before(guard_function, "ChannelMessageSend")
nk.register_rt_before(guard_function, "ChannelMessageUpdate")
nk.register_rt_before(guard_function, "ChannelMessageRemove")
nk.register_rt_before(guard_function, "MatchCreate")
nk.register_rt_before(guard_function, "MatchDataSend")
nk.register_rt_before(guard_function, "MatchJoin")
nk.register_rt_before(guard_function, "MatchLeave")
nk.register_rt_before(guard_function, "MatchmakerAdd")
nk.register_rt_before(guard_function, "MatchmakerRemove")
nk.register_rt_before(guard_function, "PartyCreate")
nk.register_rt_before(guard_function, "PartyJoin")
nk.register_rt_before(guard_function, "PartyLeave")
nk.register_rt_before(guard_function, "PartyPromote")
nk.register_rt_before(guard_function, "PartyAccept")
nk.register_rt_before(guard_function, "PartyRemove")
nk.register_rt_before(guard_function, "PartyClose")
nk.register_rt_before(guard_function, "PartyJoinRequestList")
nk.register_rt_before(guard_function, "PartyMatchmakerAdd")
nk.register_rt_before(guard_function, "PartyMatchmakerRemove")
nk.register_rt_before(guard_function, "PartyDataSend")
nk.register_rt_before(guard_function, "StatusFollow")
nk.register_rt_before(guard_function, "StatusUnfollow")
nk.register_rt_before(guard_function, "StatusUpdate")

nk.register_req_before(guard_function, "AddFriends")
nk.register_req_before(guard_function, "AddGroupUsers")
nk.register_req_before(guard_function, "AuthenticateCustom")
nk.register_req_before(guard_function, "AuthenticateDevice")
nk.register_req_before(guard_function, "AuthenticateEmail")
nk.register_req_before(guard_function, "AuthenticateFacebook")
nk.register_req_before(guard_function, "AuthenticateGameCenter")
nk.register_req_before(guard_function, "AuthenticateGoogle")
nk.register_req_before(guard_function, "AuthenticateSteam")
nk.register_req_before(guard_function, "BlockFriends")
nk.register_req_before(guard_function, "CreateGroup")
nk.register_req_before(guard_function, "DeleteAccount")
nk.register_req_before(guard_function, "DeleteFriends")
nk.register_req_before(guard_function, "DeleteGroup")
nk.register_req_before(guard_function, "DeleteLeaderboardRecord")
nk.register_req_before(guard_function, "DeleteNotifications")
nk.register_req_before(guard_function, "DeleteStorageObjects")
nk.register_req_before(guard_function, "GetAccount")
nk.register_req_before(guard_function, "GetUsers")
nk.register_req_before(guard_function, "Healthcheck")
nk.register_req_before(guard_function, "ImportFacebookFriends")
nk.register_req_before(guard_function, "JoinGroup")
nk.register_req_before(guard_function, "KickGroupUsers")
nk.register_req_before(guard_function, "LeaveGroup")
nk.register_req_before(guard_function, "LinkCustom")
nk.register_req_before(guard_function, "LinkDevice")
nk.register_req_before(guard_function, "LinkEmail")
nk.register_req_before(guard_function, "LinkFacebook")
nk.register_req_before(guard_function, "LinkGameCenter")
nk.register_req_before(guard_function, "LinkGoogle")
nk.register_req_before(guard_function, "LinkSteam")
nk.register_req_before(guard_function, "ListChannelMessages")
nk.register_req_before(guard_function, "ListFriends")
nk.register_req_before(guard_function, "ListGroups")
nk.register_req_before(guard_function, "ListGroupUsers")
nk.register_req_before(guard_function, "ListLeaderboardRecords")
nk.register_req_before(guard_function, "ListMatches")
nk.register_req_before(guard_function, "ListNotifications")
nk.register_req_before(guard_function, "ListStorageObjects")
nk.register_req_before(guard_function, "ListUserGroups")
nk.register_req_before(guard_function, "PromoteGroupUsers")
nk.register_req_before(guard_function, "DemoteGroupUsers")
nk.register_req_before(guard_function, "ReadStorageObjects")
nk.register_req_before(guard_function, "UnlinkCustom")
nk.register_req_before(guard_function, "UnlinkDevice")
nk.register_req_before(guard_function, "UnlinkEmail")
nk.register_req_before(guard_function, "UnlinkFacebook")
nk.register_req_before(guard_function, "UnlinkGameCenter")
nk.register_req_before(guard_function, "UnlinkGoogle")
nk.register_req_before(guard_function, "UnlinkSteam")
nk.register_req_before(guard_function, "UpdateAccount")
nk.register_req_before(guard_function, "UpdateGroup")
nk.register_req_before(guard_function, "WriteLeaderboardRecord")
nk.register_req_before(guard_function, "WriteStorageObjects")
It is not possible to return null to guard against the registerBeforeGetAccount function in the TypeScript server runtime. If you do need to guard this function in TypeScript, you can instead throw an error e.g. throw new Error("Function disabled"). Please be aware that this will generate error logs in Nakama.