Skip to content

t1k:nakama:auth

FieldValue
Modulebase
Version1.11.11
Effortlow
Tools—

Keywords: auth, authenticate, authentication, custom auth, firebase, provider

/t1k:nakama:auth

Authentication uses an adapter pattern with a Provider interface. The factory reads env vars to select the active provider. Before-hooks intercept authentication requests to validate external tokens, and after-hooks handle new user creation.

type Provider interface {
VerifyToken(ctx context.Context, token string) (VerifiedClaims, error)
}
type VerifiedClaims struct {
UserID string
Email string
// ... other claims
}
func NewProvider(ctx context.Context, env map[string]string, logger runtime.Logger) (auth.Provider, error) {
if env["FIREBASE_AUTH_ENABLED"] == "true" {
return firebase.New(ctx, env, logger)
}
// ... other providers
}
initializer.RegisterBeforeAuthenticateCustom(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateCustomRequest) (*api.AuthenticateCustomRequest, error) {
// Validate token via Provider
claims, err := provider.VerifyToken(ctx, in.Account.Id)
if err != nil {
return nil, runtime.NewError("invalid token", 16) // UNAUTHENTICATED
}
// Modify request with verified claims
return in, nil
})
initializer.RegisterAfterAuthenticateCustom(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, out *api.Session, in *api.AuthenticateCustomRequest) error {
if out.Created {
// Initialize new user data
}
return nil
})
  • Firebase Admin SDK init: Initialize once at plugin load (InitModule), not per-request. Repeated init causes memory leaks.
  • Provider implements auth.Provider interface
  • Factory handles provider selection via env vars
  • Before-hook validates tokens and maps errors correctly
  • After-hook initializes new user data on out.Created
  • Unwanted auth methods blocked (return nil, error)
  • Token verification errors return UNAUTHENTICATED (code 16)