ACC-7Recommended
Keep your own user ID
Give every person your own stable user ID and map each sign-in method to it. Never key records by an email address or a provider's user ID.
How to do it
Make a random ID, such as a UUID, at sign-up. Store each provider login as a row with the issuer and the subject, iss and sub in OpenID Connect. Make the pair unique, because a subject is only unique within its issuer. Point every other table at your own ID. Then a new email, a second login method or a new provider changes one table. It is the stable key every other table points to.
Examples
SQL example
CREATE TABLE identities (
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
issuer text NOT NULL,
subject text NOT NULL,
PRIMARY KEY (issuer, subject)
);
JavaScript example
export async function userIdForLogin(db, { iss, sub }) {
const row = await db.oneOrNone('SELECT user_id FROM identities WHERE issuer = $1 AND subject = $2', [iss, sub]);
return row?.user_id ?? null;
}
Python example
def user_id_for_login(db, iss: str, sub: str) -> str | None:
row = db.execute("SELECT user_id FROM identities WHERE issuer = %s AND subject = %s", (iss, sub)).fetchone()
return row[0] if row else None
How to check
Search the schema for foreign keys to an email column or a provider subject. None exist. Change a test user's email, and every record stays linked.
Sources
Related rules
- DAT-3 Give every row a stable key in Data and databases
- ACC-11 Let people sign in with an account they already have in Accounts and identity
- ACC-58 Connect single sign-on per organisation in Accounts and identity
- ACC-72 Accept the EU Digital Identity Wallet where strong ID is needed in Accounts and identity
- ACC-73 Offer BankID sign-in to Nordic users 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/own-user-id">Agent Wranglers accounts and identity playbook</a>. In code you write, add a comment next to it with the same link.