@kaaarel/auth
Docs: Account settings
View .md

Account settings

Build the settings page in your app. Call these from server functions.

await auth.account.reauthenticate({ password });
await auth.account.changePassword({ current, next });
await auth.account.changeEmail({ email, password });
await auth.account.linked();                    // { hasPassword, accounts: [{ provider, email, createdAt }] }
await auth.account.unlink({ provider: "github" });
await auth.account.delete({ password });
Function Notes Errors
reauthenticate Checks the password and opens a new 15-minute window for sensitive changes. invalid_credentials, reauth_required (the user has no password: sign in again)
changePassword current is needed when the user has a password. A Google-only user sets a first password without it, but needs a recent sign-in. Signs out the other sessions, cancels pending e-mail changes, and e-mails a notice. invalid_credentials, reauth_required, weak_password, password_too_long, rate_limited
changeEmail Needs a recent sign-in or password. Sends a link to the new address. The e-mail changes when that link is opened in a browser signed in as this user; then the old address gets a notice. email_taken, invalid_email, invalid_credentials, reauth_required, rate_limited
unlink Needs a recent sign-in or password. Refuses to remove the last way to sign in. last_sign_in_method, reauth_required
delete Needs a recent sign-in or password. Deletes the user and their data in the auth schema. Workspaces where they are the only member go too. invalid_credentials, reauth_required, transfer_ownership_first

Recent sign-in

Some changes add a new way into the account, or remove the owner's. A stolen session cookie must not be enough for them, so they need a sign-in in the last 15 minutes:

  • changeEmail, unlink, delete, and changePassword without current;
  • linking Google or GitHub, tokens.create, twoFactor.setup.

Each of these also takes an optional password. A right password counts as a fresh sign-in. On reauth_required, ask for the password and call reauthenticate (or pass password to the same call). A user without a password signs in again with their provider.

All of them return not_signed_in without a session, and forbidden for a request with an API token.

Your own user data

Keep app data in your own tables and point to auth.users(id):

create table profiles (
  user_id uuid primary key references auth.users(id) on delete cascade,
  plan text not null default 'free'
);

To create rows when a user signs up, use onUserCreated. It runs in the same transaction:

onUserCreated: async (user, tx) => {
  await tx.query("insert into profiles (user_id) values ($1)", [user.id]);
},

Admin and scripts

auth.users needs no request:

const owner = await auth.users.create({ email: "me@example.com", password: "…" });
await auth.users.byEmail("me@example.com");
await auth.users.get(id);
await auth.users.setPassword(id, "…"); // also ends their sessions, API tokens and pending e-mail changes
await auth.users.delete(id);