Authentication
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.
- While no user exists, the admin opens a sign-up page. The person who signs up becomes the first admin.
- After that, sign-up is closed. New users join by invite. See Users and roles.
- 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:
| Method | Turn it on | Default | Needs |
|---|---|---|---|
| Password | On unless emailAndPassword.enabled is false | on | – |
| Passkey | Add the passkey() plugin | off | @better-auth/passkey |
| Magic link | Add the magicLink() plugin | off | |
| Email code | Add the emailOTP() plugin | off |
Password sign-in is on by default. better-auth's emailAndPassword options change its rules:
export default defineCms({
// ...
auth: {
emailAndPassword: { minPasswordLength: 12 },
},
});
Forgot password? on the sign-in page sends a reset link. It needs email.
A passkey signs in with the fingerprint, the face or the PIN of a device. Install the plugin first:
npm install @better-auth/passkey
import { passkey } from "@better-auth/passkey";
export default defineCms({
// ...
auth: {
plugins: [passkey()],
},
});
- Users add passkeys in Account settings. Sign in with a passkey needs no email.
- When the admin runs on a subdomain of the site, pass the site's domain:
passkey({ rpID: "example.com" }). - The plugin is off by default because it adds about 600 KiB to the server bundle.
A magic link is a sign-in link in an email. It is valid for 5 minutes:
import { magicLink } from "tenoncms/auth";
export default defineCms({
// ...
auth: {
plugins: [magicLink()],
},
email: { adapter, from: "Site <hello@example.com>" },
});
The link goes only to existing users. For an unknown address, the page answers the same, but sends nothing.
An email code is a 6-digit code in an email:
import { emailOTP } from "tenoncms/auth";
export default defineCms({
// ...
auth: {
plugins: [emailOTP({ otpLength: 8 })],
},
email: { adapter, from: "Site <hello@example.com>" },
});
npm create tenoncmsasks which methods to use and writes this config for you.magicLink()andemailOTP()fromtenoncms/authare better-auth's plugins with sign-up turned off, so they only sign in existing users.- They send the built-in
magicLinkandemailOtpemails. 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()ortenon users create. - Without email, magic links and email codes are off. Tenon warns at startup when they are the only methods.
Without a password
With emailAndPassword: { enabled: false }, a new account needs only a name and an email:
- The first admin, or an invited user, enters a name and an email.
- 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.
- 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.
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.urlin production. It is the default ofauth.baseURL. See Site URL. - Another origin, like
cms.example.com: also setauth.baseURLto 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
sessionsets how long a session lasts. See Session management.rateLimitlimits 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 ownadmin()inauth.pluginsreplaces 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:
| Call | Does |
|---|---|
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 }.
Related resources
- Access control: roles, invites and rules.
- API keys: access for scripts and other services.
- Email: set up email for resets, invites and sign-in links.
- tenon users: create, recover and remove users from the command line.
- better-auth docs: all options of
auth.