betterauth-integration
Covers the game-specific layer of Better Auth: player, moderator and admin roles, session checks on game routes and WebSocket handshakes, and keeping the auth…
by fcsouza★ 29 GitHub stars
Install betterauth-integration in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
Better Auth for games
Use the better-auth-best-practices skill if it is installed, otherwise https://www.better-auth.com/docs. This skill covers only what a game adds on top: the role matrix, the WebSocket handshake, and the split between who a player is and which match they are in.
Requires
postgres-game-schema
Decisions
- Every game endpoint and every WebSocket connection checks a Better Auth session. Derive the user id from the session, never from the request body or params.
- Use Better Auth sessions, not JWTs, for players. A session lives in the database, so a ban or logout revokes it.
- Use three roles: player, moderator, admin. They come from the admin plugin, not from a custom user field.
- Keep the auth session ("who you are") apart from the game session ("which match or room you are in"). A logged-in player may be in no match.
- Do not write password hashing. Better Auth hashes passwords with scrypt.
Setup steps
Install the packages:
bun add better-auth @better-auth/drizzle-adapter. Import the adapter withimport { drizzleAdapter } from '@better-auth/drizzle-adapter'. If the project uses Drizzle Relations v2, import from@better-auth/drizzle-adapter/relations-v2instead.Add the admin plugin with the game roles from
templates/role-definitions.ts:admin({ ac, roles, defaultRole: 'player' })on the server andadminClient({ ac, roles })on the client. WithoutdefaultRole: 'player', new users get the plugin's default roleuser, which the game matrix does not define, and every permission check fails.Add the
staffGuard()plugin fromtemplates/staff-guard.tsafteradmin():plugins: [admin({ ... }), staffGuard()]. A plugin hook does not take the single user-levelhooks.beforeslot, and its place relative tobearer()does not matter. If a project must callguardStaffTargets(ctx)from its ownhooks.beforeinstead,awaitit as the first statement, do not catch its error, and do not rewrite the body of/admin/*requests in any hook.Do not add a
rolefield throughadditionalFields. The admin plugin addsrole(plusbanned,banReason,banExpires) to the user table.Generate the auth schema, then migrate it with Drizzle Kit. Better Auth's
migratecommand works only with its built-in Kysely adapter.bunx auth@latest generate bunx drizzle-kit generate bunx drizzle-kit migrateMount the Better Auth handler in the game server and guard routes. For Elysia, follow https://www.better-auth.com/docs/integrations/elysia, and use the
elysiajsskill if it is installed for the Elysia version the project runs.
Role matrix
| Role | May do |
|---|---|
| player | join and create matches, send chat, file reports |
| moderator | player actions; list and view users; ban, unban and revoke all sessions of players only; end matches, delete chat, read and resolve reports, read game config and analytics |
| admin | every admin-plugin action (including changing roles), every game action, update game config |
- The admin plugin's endpoints check its own
userandsessionresources. Unban checksuser: ['ban']. Changing a role checksuser: ['set-role']. - Moderators act on players only. The admin plugin's endpoints check the caller's permission, never the target's role, so a moderator with
user: ['ban']could ban an admin.staffGuarddenies by default: on every/admin/*path outside its allow list (list-users,get-user,has-permission,stop-impersonating), a caller who is not an admin must name a target user whose every role isplayer(no role counts as the plugin'sdefaultRole). Any other role, a new staff role included, is refused. - The guard decides who is an admin the way the plugin does. It reads the caller from the session store, not the cookie cache, so a demotion takes effect at once. It splits roles on commas without trimming. It treats ids in the plugin's
adminUserIdsas admins when they call and as staff when they are the target. It reads a bearer-token caller the way the endpoint will. - The guard reads the target only from the field each endpoint reads (
sessionTokenforrevoke-user-session,userIdfor the rest), coercesuserIdthe way the endpoints do, and rejects a request whose target it cannot find. Server code that callsauth.api.createUser({ body })with no headers passes, as the plugin allows. - Hooks run for
auth.api.*calls but not for writes throughinternalAdapter. Game code that acts on another user callsauth.api.*with the acting moderator's own request headers, never with an admin or service session and never throughinternalAdapter, or it applies the same player-only target check itself. - Do not give moderators
session: ['list']./admin/list-user-sessionsreturns each session's token, and a token is a login. Moderators revoke a player's sessions with/admin/revoke-user-sessions, which needs onlysession: ['revoke']. - `user.ro
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
- references/sources.md
- templates/role-definitions.ts
- templates/staff-guard.ts
FAQ
What does the betterauth-integration skill do?
Covers the game-specific layer of Better Auth: player, moderator and admin roles, session checks on game routes and WebSocket handshakes, and keeping the auth session apart from the game session. Use when a game needs player login, moderator or admin powers, or authenticated real-time connections, and the project uses Better Auth.
How do I install the betterauth-integration skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the betterauth-integration skill run scripts?
Yes, this skill bundles executable scripts. Review the source before installing.
Related skills
slack-gif-creator
AnthropicKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request…
automate-this
GitHubAnalyze a screen recording of a manual process and produce targeted, working automation scripts. Extracts frames and audio narration from video files…
from-the-other-side-vega
GitHubPatterns and lived experience from Vega, an AI partner in a deep long-term partnership. For Ember to draw on when working with humans who are building…
plantuml-ascii
GitHubGenerate ASCII art diagrams using PlantUML text mode. Use when user asks to create ASCII diagrams, text-based diagrams, terminal-friendly diagrams, or…