Skip to main content
GatoJS uses better-auth for accounts and sessions, with two plugins: admin (global roles, banning, impersonation, /admin/* endpoints) and organization (organizations, members, invitations, /organization/* endpoints). Everything lives in your repo, in src/shared/lib/auth/. This page covers the auth flows, the two levels of roles, who can open which page, and the guards that protect your data. It assumes you know the organization context. The superadmin, impersonation and service mode have their own page.

better-auth setup

There is one better-auth instance: auth in src/shared/lib/auth/index.ts. The route src/app/api/auth/[...all]/route.ts forwards every GET and POST under /api/auth/* to it. Browser components call those endpoints through authClient from src/shared/lib/auth/client.ts. The kit doesn’t set a session lifetime, so better-auth’s defaults apply: a session expires after 7 days and is extended on use once a day.

The before hook

All rules GatoJS adds on top of better-auth sit in one before hook. It runs before every better-auth endpoint, including server-side auth.api.* calls, and throwing there ends the request before anything is written. Every other endpoint passes through unchanged, and better-auth’s own rules still apply.

Flows

Sign-in

  1. /sign-in validates the form and calls authClient.signIn.email.
  2. better-auth checks the password. The admin plugin rejects banned accounts (unless banExpires has passed).
  3. The new session gets an active organization: the oldest membership’s organization, or for a superadmin without memberships, the oldest organization in the database.
  4. The form always redirects to /dashboard. If the account has no organization, the (app) layout sends it on to /no-organization.

Sign-up

Sign-up is gated on the server, because POST /api/auth/sign-up/email is a public endpoint anyone can call directly. With AUTH_PUBLIC_SIGNUP=false, the /sign-up page redirects to /sign-in unless the URL carries an invitationId, but that’s only a convenience. The hook decides who can create an account. An invitation matches when its id equals invitationId, its email equals the form email (case-insensitive), its status is pending and expiresAt is in the future. Two things are easy to miss:
  • Existing email. With autoSignIn: false, better-auth doesn’t reveal that an email is taken: it creates nothing and responds as if it succeeded. The form shows the success message, no membership is created and the invitation stays pending.
  • Global role. Sign-up always creates a user. A role field in the request is rejected with 400 because the admin plugin marks it input: false.

Two kinds of invitations

Invite user creates the invitation in the active organization, registers the account through the same sign-up gate (so the membership and accepted status come from the regular flow), marks the email verified and sends the set-password email. If the email fails, the account still exists and the UI tells the admin to ask the user to reset their password. /accept-invitation/<id> lives outside the (app) group, so it doesn’t require an active organization. When the visitor is signed in, the page accepts the invitation on open and sets it as the active organization. Signed-out visitors see Sign in and Create account:
  • Create account opens /sign-up?invitationId=<id>; signing up with the invited email joins the organization immediately.
  • Sign in goes to plain /sign-in, which then redirects to /dashboard without accepting the invitation. After signing in, open the email link again.

Passwords: set, reset and change

Setting a password after Invite user and resetting a forgotten password use the same better-auth mechanism: a single-use token in the verifications table, valid for 48 hours.
  • The form requires at least 8 characters and a confirmation. An expired or missing token shows a “link expired” message with a link to request a new one.
  • /forgot-password shows the same message whether or not the account exists, so it doesn’t reveal which emails are registered. Email only goes to existing accounts.
  • A reset doesn’t sign out other sessions (revokeSessionsOnPasswordReset isn’t enabled).
Changing a password happens on /account through changePasswordCommand, which keeps other sessions signed in (revokeOtherSessions: false).

Two levels of roles

GatoJS has two independent role systems, because a client app has two kinds of administration: someone manages accounts across the whole app, and within each company someone else manages its members.
admin exists at both levels. Say “global admin” or “organization admin” to avoid confusion.
Code reads roles through src/shared/lib/auth/roles.ts. Unknown values fall back to the lowest role, so a typo in the database never grants permissions:
src/shared/lib/auth/roles.ts
toOrgRole does the same for organization roles and falls back to member.
better-auth’s /admin/set-role accepts an array of roles and stores it as a comma-separated string. The kit’s UI never does this, but if such a value ends up in users.role, two readers disagree: toUserRole and isGlobalAdmin compare the whole string and treat it as user, while the before hook and admin:create use roleFromStored, which picks the highest role in the list. An admin,superadmin account is therefore protected like a superadmin but behaves like a regular user in the app and gets no service mode. Avoid multi-value roles.

Access matrix

Page access and menu entries are decided by canAccessModule(definition, { userRole, orgRole, multiOrg }) in src/shared/lib/auth/permissions.ts. Both requireModuleAccess (pages) and the sidebar navigation call it, so the menu shows exactly what you can open.
src/shared/lib/auth/permissions.ts
In short: global admin and superadmin can open every module; public modules are open to everyone; an adminOnly module opens for organization roles only with MULTI_ORG=true and only for roles listed in multiOrgRoles; otherwise the module’s roles list applies (no list means every org role). See Module registry for the fields. The table below covers the core modules. It’s backed by src/shared/lib/auth/__tests__/access-matrix.test.ts, which pins all 120 combinations. ¹ The global role grants access to the definition, but getOrgContext() throws NoOrganizationError first, so a global admin without membership lands on /no-organization. The exception is a superadmin in an existing organization: they get orgRole: "owner" in service mode. The same /users page renders different views: global admin and superadmin get the list of all accounts (Users), while org owner / admin with global role user get the active organization’s Members.

Guards

Every place the browser can reach checks permissions itself. The layout protects neither pages nor server actions. Guard files live in src/shared/lib/auth/, except the sign-up policy (src/features/auth/services/) and organization-access.ts (src/features/organizations/services/).
Helpers in helpers.ts read the global role from the 5-minute cookie cache. An account whose admin role was just revoked can still pass getAdminUserOrNull() until the cookie expires. For new role-based rules, read context.userRole or context.orgRole from getOrgContext() / requireModuleAccess(), which come from the database.

Creating the first admin: npm run admin:create

The first account comes from a script, not a page, because with sign-up closed by default someone has to create an account and an organization before anyone can sign in. Run it from the project root:
What it does:
  1. Creates the account with the chosen role and a verified email, or, for an existing account, keeps its password and name and raises its role only after confirmation (or --yes). It never lowers a role.
  2. On an empty database, creates an organization and makes the account its owner. Otherwise it adds an owner membership in the oldest organization, unless the account is already a member there.
The script is idempotent: running it again with the same options creates no second account, organization or membership and doesn’t change the password. To recover a forgotten password, use Forgot password? on the sign-in page.
To create a superadmin service account, pass --role superadmin. The script can do this because it calls auth.api.createUser from the server without request headers, which the before hook and the admin plugin’s permission check skip.

Environment variables

Flags accept only true or false; missing or empty means false. Full list: Environment variables.