Files
KomediruzeckiandGitHub 76e1efded8 feat(auth): a password account confirms its email before it uses the app (#616)
A password account confirms its email before it signs in or uses the app.

- Create account ends on Check your inbox, which names the address, sends the link again and says
  where to open it. The screen is the same whatever the address.
- A password sign-in works once the address is confirmed, or in the browser that opened the
  account's own confirm link. Until then the right password is refused as a wrong one is, and the
  form offers Send the link again.
- A signed-in account whose address is not confirmed sees Confirm your email instead of the app.
  The Worker answers its sessions and API tokens 403 EMAIL_UNCONFIRMED everywhere but me, logout,
  the signed-in resend, the link's finish route, and the billing portal for an account that pays.
- POST /api/auth/verify-email/resend sends the link again while signed out, with limits and the
  captcha before any look-up. One budget of three confirm links an hour covers every way of
  asking for one.
- Sign-up answers first, then makes the account, its profile and its first link in one batch,
  tried twice. Signing up again for an address waiting for its link mails a fresh link.
- A sign-in code, a reset link and Google sign-in confirm the address. A passkey or two-factor
  does not.
- /api/v1 capabilities, reminders and sheet syncs leave out an account waiting for its link.
- The welcome and confirm mails say the link starts the account and where to sign in after
  opening it. No mail on the sign-up path has an em dash before the shared footer.

No migration.
2026-10-10 18:17:51 +02:00

45 lines
1.8 KiB
TypeScript

/**
* A signed-in session for a test, issued the way a sign-in route issues one: bound to the
* account's token_version as it stands now. The account must exist.
*
* sessionCookie confirms the account's address first. A password account whose address is not
* confirmed reaches only the routes confirming it needs (requireAuth answers the rest 403
* EMAIL_UNCONFIRMED), and most tests insert their account without saying either way, which
* leaves it unconfirmed. A test about an account waiting for its link uses
* unconfirmedSessionCookie, which leaves the account as it is.
*/
import { env } from 'cloudflare:test';
import { issueSessionCookie, type SessionOrigin } from '../../src/auth';
import type { Env } from '../../src/index';
/** The whole Set-Cookie header for a new session of `userId`, whose address is confirmed first. */
export async function sessionCookie(
userId: number,
provider = 'password',
e: Env = env as unknown as Env,
origin: SessionOrigin = {}
): Promise<string> {
await e.DB.prepare('UPDATE users SET email_verified = 1 WHERE id = ?').bind(userId).run();
return unconfirmedSessionCookie(userId, provider, e, origin);
}
/** The whole Set-Cookie header for a new session of `userId`, with its address left as it is. */
export async function unconfirmedSessionCookie(
userId: number,
provider = 'password',
e: Env = env as unknown as Env,
origin: SessionOrigin = {}
): Promise<string> {
const row = await e.DB.prepare('SELECT token_version FROM users WHERE id = ?')
.bind(userId)
.first<{ token_version: number }>();
if (!row) throw new Error(`sessionCookie: there is no user ${userId}`);
const cookie = await issueSessionCookie(
e,
{ userId, provider, tokenVersion: row.token_version },
origin
);
if (!cookie) throw new Error(`sessionCookie: no session was written for user ${userId}`);
return cookie;
}