Skip to content

Show the last sign-in method

For: an app developer whose sign-in page offers several methods, and who wants “Last time you used Google”.

What it does: with lastMethod, each browser sign-in sets the cookie __Host-tempo_last_method with one value of a closed set. A script of the sign-in page reads it. Core never stores the method.

  1. Set lastMethod to {}, or to an object with cookieName and days.
  2. On the sign-in page, call client.lastMethod() of the client package. It reads the cookie through isLastMethod, so a value outside the closed set reads as null. Without the client, read document.cookie and check the value with isLastMethod of @authtempo/core/routes.
  3. Show the button of that method first, or mark it.
  4. In a native app, read via from the JSON answer of the sign-in, and keep it yourself.
The last method in the config
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** A hint on the sign-in page: "Last time you used …". */
export function createLastMethodAuth(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,
magicLink: {},
password: {},
// The defaults: the cookie __Host-tempo_last_method, kept 30 days.
lastMethod: { days: 30 },
});
}
/** The browser side: read the cookie, and mark the button of that method. */
export function lastMethodOf(cookie: string): string | null {
const pair = cookie.split("; ").find((c) => c.startsWith("__Host-tempo_last_method="));
return pair ? decodeURIComponent(pair.slice(pair.indexOf("=") + 1)) : null;
}
The cookie as a test
import { describe, expect, it } from "vitest";
import { call, harness, linkIn } from "../support/harness.ts";
import { createLastMethodAuth, lastMethodOf } from "./last-method.ts";
describe("the last method cookie", () => {
it("names the method of the last browser sign-in, in a cookie that a script reads", async () => {
const h = harness();
const auth = createLastMethodAuth(h);
await call(auth, "POST", "/auth/magic-link/request", { email: "ada@example.com" });
const token = linkIn(h, "ada@example.com").searchParams.get("token") ?? "";
const res = await call(auth, "POST", "/auth/magic-link/verify", { token });
const line =
res.headers.getSetCookie().find((c) => c.startsWith("__Host-tempo_last_method=")) ?? "";
expect(line).not.toContain("HttpOnly");
expect(lastMethodOf(line.split(";")[0] ?? "")).toBe("magic_link");
expect(((await res.json()) as { via: string }).via).toBe("magic_link");
});
it("sets no cookie for a bearer client", async () => {
const auth = createLastMethodAuth(harness());
await call(auth, "POST", "/auth/password/sign-up", {
email: "bo@example.com",
password: "correct horse battery",
});
const res = await call(
auth,
"POST",
"/auth/password/sign-in",
{ email: "bo@example.com", password: "correct horse battery" },
{ authorization: "Bearer ts_unknown" },
);
expect(res.status).toBe(200);
expect(res.headers.getSetCookie().some((c) => c.includes("tempo_last_method"))).toBe(false);
});
});
  • The value is one of password, username, phone, magic_link, email_otp, passkey, or social:<provider>. A password sign-in by username gives username, and by phone gives phone.
  • The cookie is not HttpOnly, so a script reads it. It has Secure, SameSite=Lax, and Path=/, and lives lastMethod.days days (30 by default, 1 to 400).
  • It holds the method only. It names no account, and it signs no one in. No read of core trusts it.
  • Each JSON sign-in answer carries the same value as via.
  • A request with the Bearer scheme gets no cookie: a bearer client keeps via. HTTP Basic in front of the site does not count as a bearer.
  • A sign-in that answers a second factor challenge sets the cookie at the challenge verify.
  • An anonymous sign-in sets no cookie and gives no via.
  • lastMethod.cookieName renames the cookie, with 1 to 64 token characters. Core refuses a name of another cookie of core.
  • The cookie tells anyone at the browser which method the last person used. It names no account, so use it as a hint only.
  • A person who clears the cookies of the site loses the hint. Core stores no copy, by design.