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.
Before you start
Section titled “Before you start”- A sign-in page with more than one method, such as Passwords and Sign in with a magic link.
- The cookie rules of Cookies.
- Set
lastMethodto{}, or to an object withcookieNameanddays. - On the sign-in page, call
client.lastMethod()of the client package. It reads the cookie throughisLastMethod, so a value outside the closed set reads asnull. Without the client, readdocument.cookieand check the value withisLastMethodof@authtempo/core/routes. - Show the button of that method first, or mark it.
- In a native app, read
viafrom the JSON answer of the sign-in, and keep it yourself.
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;}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); });});How it behaves
Section titled “How it behaves”- The value is one of
password,username,phone,magic_link,email_otp,passkey, orsocial:<provider>. A password sign-in by username givesusername, and by phone givesphone. - The cookie is not
HttpOnly, so a script reads it. It hasSecure,SameSite=Lax, andPath=/, and liveslastMethod.daysdays (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.cookieNamerenames the cookie, with 1 to 64 token characters. Core refuses a name of another cookie of core.
Residuals
Section titled “Residuals”- 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.
If it fails
Section titled “If it fails”invalid_credentials: the sign-in failed, so no cookie was set.unauthorized: the request had no session. The cookie stays from the last sign-in.