ACC-16Recommended
Never remove the last way to sign in
Let people add and remove sign-in methods, but refuse to remove the last one.
How to do it
List every passkey, password, provider login and second factor on one settings page, with the date each was added and last used. Count the remaining methods inside the same transaction as the removal. Count only methods that sign someone in alone, because a second factor cannot. Link new provider logins as link a provider login only to an account its owner controls says. Ask for fresh proof first, as ask again before changing how someone logs in says.
Examples
JavaScript example
const LAST_METHOD = 'You need at least one way to sign in. Add another before removing this one.';
export async function removeSignInMethod(db, userId, methodId) {
await db.tx(async tx => {
await tx.none('SELECT 1 FROM users WHERE id = $1 FOR UPDATE', [userId]);
const { n } = await tx.one('SELECT count(*)::int AS n FROM sign_in_methods WHERE user_id = $1', [userId]);
if (n <= 1) throw new ConflictError(LAST_METHOD);
await tx.none('DELETE FROM sign_in_methods WHERE id = $1 AND user_id = $2', [methodId, userId]);
});
}
Python example
LAST_METHOD = "You need at least one way to sign in. Add another before removing this one."
def remove_sign_in_method(conn, user_id: str, method_id: str) -> None:
with conn.transaction():
conn.execute("SELECT 1 FROM users WHERE id = %s FOR UPDATE", (user_id,))
(count,) = conn.execute("SELECT count(*) FROM sign_in_methods WHERE user_id = %s", (user_id,)).fetchone()
if count <= 1:
raise Conflict(LAST_METHOD)
conn.execute("DELETE FROM sign_in_methods WHERE id = %s AND user_id = %s", (method_id, user_id))
How to check
On a test account with one passkey only, call the remove endpoint for it. The answer is 409 and the passkey stays.
Sources
Related rules
- ACC-3 Stop strangers from claiming an address before its owner in Accounts and identity
- ACC-71 Let people run an account on passkeys alone in Accounts and identity
- ACC-12 Link a provider login only to an account its owner controls in Accounts and identity
- ACC-78 Ask again before changing how someone logs in in Accounts and identity
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/accounts/sign-in-methods/last-sign-in-method">Agent Wranglers accounts and identity playbook</a>. In code you write, add a comment next to it with the same link.