@kaaarel/auth
Docs: E-mail and password
View .md

E-mail and password

Turn it on with password() in methods. Passwords are hashed with argon2id (19 MiB, 2 passes), the OWASP minimum.

Sign up

const result = await auth.password.signUp({ email, password, name });
Result Meaning
{ ok: true, next: "verify_email" } Show "Check your inbox". The e-mail has a link to /auth/verify.
{ ok: true, next: "signed_in" } Only with verifyEmail: false. The session cookie is set.
{ ok: false, error } invalid_email, weak_password, password_too_long, email_taken, signup_closed, rate_limited, forbidden_origin

With verifyEmail on, sign-up with an e-mail that already has an account also returns verify_email. The owner gets a "you already have an account" e-mail. So nobody can use the sign-up form to test which e-mails have accounts. email_taken comes only with verifyEmail: false.

The verification link works once and for 24 hours. Opened in the browser that signed up, it signs the user in and redirects to pages.verified. Opened anywhere else (another device, a forwarded e-mail), it only verifies the e-mail and redirects to pages.signIn?verified=1; show "E-mail confirmed, sign in" there.

Sign in

const result = await auth.password.signIn({ email, password, next: "/app/deals" });
if (result.ok) navigate({ href: result.redirectTo });
Result Meaning
{ ok: true, next: "signed_in", redirectTo } Signed in. redirectTo is next when it is a path on this site, else pages.afterSignIn.
{ ok: true, next: "two_factor", redirectTo } The user has 2FA on. redirectTo is pages.twoFactor. See Two-factor.
{ ok: false, error } invalid_credentials, email_not_verified, access_denied, rate_limited, forbidden_origin
  • A wrong password and an unknown e-mail both give invalid_credentials. Both take the same time.
  • email_not_verified comes only after the right password. It also sends a new link (at most 3 per hour).
  • Limits: 10 failed attempts per e-mail and 50 attempts per IP in 15 minutes. The right password still works while someone guesses, until the 10th failure.

Password reset

Two pages in your app: "forgot password" and "new password".

// Forgot password page
await auth.password.requestReset({ email }); // always { ok: true }

// New password page, at pages.resetPassword?token=…
const result = await auth.password.reset({ token, password });
  • requestReset always answers { ok: true }, so it does not show which e-mails have accounts.
  • Each link works once and for 1 hour. A new request does not cancel older links, so a stranger cannot cancel the owner's link. At most 3 reset e-mails per hour go to one address.
  • reset errors: invalid_token, link_expired, weak_password, password_too_long.
  • A reset signs out every session, deletes every API token and cancels pending e-mail changes. Send the user to the login page after it.
  • A reset also marks the e-mail as verified, because the link reached that inbox.

Resend the verification e-mail

await auth.password.resendVerification({ email }); // always { ok: true }

Users without a password

A user who signed up with Google has no password. They can set one with auth.account.changePassword({ next }) or with a password reset.