Users and Access

Authentication

How people sign in to the admin, and the methods to pick from.

Editors sign in to the admin with an email and a password by default. You can add passkeys, sign-in links and sign-in codes, or turn passwords off. This page explains how sign-in works, how to pick the methods, and what to do when nobody can sign in.

Tenon builds on better-auth. The auth option of defineCms() takes better-auth's options, so its plugins and settings (should) work here too. If the better-auth plugins require some kind of ui, you will likely need to add it yourself through a plugin. (Guide will follow)

How sign-in works

The admin's sign-in page is /admin/login.

  1. While no user exists, the admin opens a sign-up page. The person who signs up becomes the first admin.
  2. After that, sign-up is closed. New users join by invite. See Users and roles.
  3. A user who signs in gets a session cookie. The admin, the live preview and the admin bar use it.

The sign-in page asks for the email first. Continue then shows the methods that are on. On the next visit, Continue goes straight to the method used last on this device.

Sign-in methods

Pick any mix of these methods:

MethodTurn it onDefaultNeeds
PasswordOn unless emailAndPassword.enabled is falseon–
PasskeyAdd the passkey() pluginoff@better-auth/passkey
Magic linkAdd the magicLink() pluginoffEmail
Email codeAdd the emailOTP() pluginoffEmail

Password sign-in is on by default. better-auth's emailAndPassword options change its rules:

tenon.config.ts
export default defineCms({
  // ...
  auth: {
    emailAndPassword: { minPasswordLength: 12 },
  },
});

Forgot password? on the sign-in page sends a reset link. It needs email.

  • npm create tenoncms asks which methods to use and writes this config for you.
  • magicLink() and emailOTP() from tenoncms/auth are better-auth's plugins with sign-up turned off, so they only sign in existing users.
  • They send the built-in magicLink and emailOtp emails. To change them, see Email.
  • A user keeps their password and sessions after their first magic link. Every user that Tenon creates counts as verified: the first admin, an invited user, and a user from cms.users.create() or tenon users create.
  • Without email, magic links and email codes are off. Tenon warns at startup when they are the only methods.
Tenon does not start without a sign-in method. When you turn passwords off, add at least one other method.

Without a password

With emailAndPassword: { enabled: false }, a new account needs only a name and an email:

  1. The first admin, or an invited user, enters a name and an email.
  2. The admin asks How do you want to sign in? The user creates a passkey, or continues by email when a magic link or an email code is on.
  3. Next time, the user signs in with that method.

A user who leaves before creating a passkey, and has no email method, signs in again with recovery.

Account settings

Account settings in the user menu lets each user:

  • change their name, the admin's language and their time zone. Dates in the admin follow the time zone.
  • change their password, or set a first one. This signs out their other sessions.
  • add and remove passkeys, when passkeys are on.

A user cannot remove their last passkey while it is their only way to sign in.

Recovery

Forgot password? sends a reset link by email. When that does not help, for example because email is not set up or the last passkey is lost, run this in the project:

npx tenon users recover anna@example.com

It prints a sign-in link, and also emails it when email is set up. It needs no session, only the database.

  • The link works once and expires after an hour.
  • It signs the user out everywhere else, signs them in, and opens Account settings to set a new password or add a passkey.
  • In server code, cms.users.signInLink(email, { origin }) returns the same link.

See tenon users for the other user commands.

Auth options

auth passes better-auth's options through. Tenon sets database, basePath and secret itself.

tenon.config.ts
import { twoFactor } from "better-auth/plugins";

export default defineCms({
  // ...
  auth: {
    session: { expiresIn: 60 * 60 * 24 * 30 },
    plugins: [twoFactor()],
  },
});

The CMS origin

Sign-in, invites, password resets and every link into the admin need the origin of the CMS:

  • The same origin as the site: set site.url in production. It is the default of auth.baseURL. See Site URL.
  • Another origin, like cms.example.com: also set auth.baseURL to that origin, without a path. For drafts and the admin bar on the site, also share the cookies. See Headless.
  • More than one host, like a preview domain: list them in allowedHosts, with a fallback for links that Tenon sends without a request.
auth: {
  baseURL: {
    allowedHosts: ["cms.example.com", "preview.example.com"],
    fallback: "https://cms.example.com",
  },
},

List only hosts you control. In development, no URL is needed: Tenon uses the URL of the dev server.

Sessions and rate limits

  • session sets how long a session lasts. See Session management.
  • rateLimit limits sign-in attempts. Tenon stores the counts in the database, because serverless functions do not share memory.

Your own better-auth plugins

Plugins in auth.plugins run like in any better-auth app. Their tables are created on the first request.

  • The admin has screens for the four sign-in methods above and for password reset. Other plugins that need screens, like two-factor sign-in, work on the server only.
  • Core adds better-auth's admin() plugin for user management. Your own admin() in auth.plugins replaces it.

Run code on sign-in

better-auth's hooks run on its routes. For example, after each email sign-in:

import { createAuthMiddleware } from "better-auth/api";

auth: {
  hooks: {
    after: createAuthMiddleware(async (ctx) => {
      if (ctx.path === "/sign-in/email") console.log("signed in");
    }),
  },
},

databaseHooks run when a user, a session or an account is written, like databaseHooks.user.create.after for a welcome email. Tenon's own check runs before your hooks:

  • The first user is always an admin, whatever role your hook returns.
  • A request that creates a later user needs the session of an admin. Server code without a request may create users.

Your own sign-in form

The client has the sign-in calls for a form of your own, outside the admin:

CallDoes
client.signIn({ email, password })Signs in with a password
client.signInMagicLink({ email, callbackURL })Sends a magic link
client.sendSignInCode({ email })Sends an email code
client.signInWithCode({ email, otp })Signs in with the code
client.requestPasswordReset({ email, redirectTo })Sends a reset link to your form
client.resetPassword({ token, newPassword })Sets the new password
client.signOut()Signs out

GET /_tenon/auth-status tells the form which methods are on: { setup, signUp, password, passkeys, magicLink, emailOtp, emailOtpLength }.