Skip to content

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.

  1. Set username to {}, or to an object with minLength, maxLength, and reserved.
  2. On the sign-up form, call GET /username/available?username=<name> as the person types.
  3. After the confirm of the address, post { username } to POST /user/username.
  4. On the sign-in form, post { username, password } to POST /password/sign-in.
  5. Show the displayUsername of the answer. Store nothing of your own about the name.
  6. In a browser app, call client.username.available and client.username.set of the client package.
The username in the config
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"],
},
});
}
The username as a test
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" });
});
});
  • The stored form is the NFKC form in lower case, and it matches ^[a-z0-9_.]+$. displayUsername keeps 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. minLength and maxLength narrow it, each from 3 to 30.
  • Core always reserves the organization slugs, root, support, security, and admin. reserved adds your names. The check reads the stored form, so Admin is reserved too.
  • POST /user/username needs 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 username only with password.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:ip 30 per 15 minutes, username:user 5 per hour, and password:username 10 per 15 minutes on the stored form.
  • An anonymous session cannot set a username: POST /user/username answers anonymous_refused.
  • A username is public by design. GET /username/available tells 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:ip bounds 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.