Sign in with a username
For: an app developer whose users have a handle, such as a forum or a game.
What it does: username adds GET /username/available and POST /user/username. POST /password/sign-in then takes username in place of email.
Before you start
Section titled “Before you start”- Password sign-in, as in Passwords.
- A fresh session for the person who sets a name, as in Fresh sessions.
- Set
usernameto{}, or to an object withminLength,maxLength, andreserved. - On the sign-up form, call
GET /username/available?username=<name>as the person types. - After the confirm of the address, post
{ username }toPOST /user/username. - On the sign-in form, post
{ username, password }toPOST /password/sign-in. - Show the
displayUsernameof the answer. Store nothing of your own about the name. - In a browser app, call
client.username.availableandclient.username.setof the client package.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** Password sign-in by address or by a public username. */export function createUsernameAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, password: {}, username: { // 3 to 30 characters after NFKC. These are the defaults. minLength: 3, maxLength: 30, // Names of your own that no account may take, in the stored form. reserved: ["billing", "staff"], }, });}import { describe, expect, it } from "vitest";import { call, callSettled, harness, linkIn, proofCookieOf, sessionCookieOf,} from "../support/harness.ts";import { createUsernameAuth } from "./username.ts";
describe("sign in with a username", () => { it("sets a username after the confirm, and signs in with it", async () => { const h = harness(); const auth = createUsernameAuth(h); const password = "correct horse battery"; const up = await callSettled(auth, "POST", "/auth/password/sign-up", { email: "ada@example.com", password, }); // The confirm link proves the address and opens a fresh session. The // browser of the sign-up sends its proof cookie, so the password stays. const token = linkIn(h, "ada@example.com").searchParams.get("token") ?? ""; const confirmed = await call( auth, "POST", "/auth/password/verify", { token }, { cookie: proofCookieOf(up) }, ); const cookie = sessionCookieOf(confirmed);
const free = await call(auth, "GET", "/auth/username/available?username=Ada_L"); expect(await free.json()).toEqual({ available: true }); const set = await call(auth, "POST", "/auth/user/username", { username: "Ada_L" }, { cookie }); expect(await set.json()).toEqual({ username: "ada_l", displayUsername: "Ada_L" });
// The sign-in reads the stored form, so the case does not matter. const res = await call(auth, "POST", "/auth/password/sign-in", { username: "ADA_L", password }); expect(res.status).toBe(200); });
it("answers invalid_credentials for an unknown name, as for a wrong password", async () => { const auth = createUsernameAuth(harness()); const res = await call(auth, "POST", "/auth/password/sign-in", { username: "nobody", password: "correct horse battery", }); expect(res.status).toBe(401); expect(await res.json()).toEqual({ error: "invalid_credentials" }); });
it("refuses a reserved name", async () => { const auth = createUsernameAuth(harness()); const res = await call(auth, "GET", "/auth/username/available?username=staff"); expect(await res.json()).toEqual({ error: "username_invalid" }); });});How it behaves
Section titled “How it behaves”- The stored form is the NFKC form in lower case, and it matches
^[a-z0-9_.]+$.displayUsernamekeeps the case that the person typed. - A username holds no
@and no+. So the sign-in never reads a username as an address or a phone. - The length is 3 to 30 characters after NFKC by default.
minLengthandmaxLengthnarrow it, each from 3 to 30. - Core always reserves the organization slugs,
root,support,security, andadmin.reservedadds your names. The check reads the stored form, soAdminis reserved too. POST /user/usernameneeds a fresh session and a proved handle: a verified address, a phone that signs in, or a passkey. A notice goes to the address of the user.- The old name is free at once. Of two people who set one name at the same time, one gets
username_taken. - The sign-up takes
usernameonly withpassword.uniformSignUp: false. With the uniform answer, a name at sign-up would tell a known address from a new one. So the person sets the name after the confirm. - A proof of the address on an unverified account clears the username that a claimant chose. Only the confirm link of the sign-up that named the name keeps it.
- The sign-in by username runs the dummy hash for an unknown name. An unknown name and a wrong password both answer
invalid_credentials. - The limits:
username-available:ip30 per 15 minutes,username:user5 per hour, andpassword:username10 per 15 minutes on the stored form. - An anonymous session cannot set a username:
POST /user/usernameanswersanonymous_refused.
Residuals
Section titled “Residuals”- A username is public by design.
GET /username/availabletells whether an account holds a name, and the name tells a guesser which account to try. - Each kind of identifier has its own limit, so the limits add up. One account takes up to 20 guesses per 15 minutes by address and username, and 30 with a phone.
password:ipbounds each IP at 20. - An unknown name reads one store query less than a wrong password. On a slow store, the time can tell that a name exists. The name is public in any case.
- A freed name has no hold period. Another person can take it at once, and old links to it then name that person.
If it fails
Section titled “If it fails”username_invalid: the name breaks the length, the character rule, or the reserved list.username_taken: another account holds the stored form of the name.invalid_credentials: the name or the password is wrong.email_unverified: the user has no proved handle. Confirm the address first.session_not_fresh: sign in again, then set the name.invalid_body: the sign-in body has two identifiers or none, or the sign-up has a name under the uniform answer.rate_limited: too many checks from the IP, sets by the user, or tries for the name.anonymous_refused: an anonymous session tried to set a name.conflict: a parallel write won five rounds of the set. Core writes the lineusername.set_gave_up. Send the name again.hook_refusedandhook_failed: a hook of your app refused the change or threw.impersonation_session: an operator who impersonates a user cannot set the name.unauthorized:POST /user/usernameneeds a session.