Files
uptimesebi/internal/migrations/sql/00044_email_verification.sql
Sebastian UnterschützandClaude Opus 5.5 87b2f2a256
ci / image (pull_request) Successful in 3m59s
ci / build (pull_request) Successful in 5m46s
ci / test (pull_request) Successful in 12m32s
ci / lint (pull_request) Successful in 14m9s
feat: verified sign-up and several organizations per account
New accounts confirm their email address before signing in: sign-up
mails a link valid for 24 hours, and its page confirms only when the
visitor presses the button, so mail scanners opening links do not sign
in. Confirming signs the account in; unconfirmed accounts are told so
only after giving the right password, can ask for a new link, and are
deleted after a week. UPTIME_SIGNUP_VERIFY requires confirmation always
in the cloud edition and self-hosted wherever mail can be sent; without
a mail server, sign-up that needs it stays closed.

One account per mailbox: users.email_key drops +suffixes, and dots for
Gmail, and is unique, including for accounts created through
invitations and SSO, which count as confirmed. A client address (IPv6:
its /64) may create UPTIME_SIGNUPS_PER_IP accounts a day, 2 by default;
refused sign-ups do not count.

Sign-up asks only for name, email and password. Accounts without an
organization are led to create one; signed-in users create further
organizations from the switcher, within the per-account limit, and
POST /api/v1/orgs does the same.

Closes #112
Closes #113

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 21:59:12 +01:00

33 lines
1.4 KiB
SQL

-- +goose Up
-- Accounts confirm their email address before signing in; existing ones
-- count as confirmed.
ALTER TABLE users ADD COLUMN email_verified_at timestamptz;
UPDATE users SET email_verified_at = created_at;
-- One account per mailbox: email_key is the address with +suffixes and,
-- for Gmail, dots removed (see emailKey in the api package). Accounts that
-- already share a key keep theirs empty; only the oldest holds it.
ALTER TABLE users ADD COLUMN email_key text;
WITH parts AS (
SELECT id, created_at,
split_part(split_part(lower(email::text), '@', 1), '+', 1) AS local,
split_part(lower(email::text), '@', 2) AS domain
FROM users
), keyed AS (
SELECT id,
CASE WHEN domain IN ('gmail.com', 'googlemail.com') THEN replace(local, '.', '') || '@gmail.com'
ELSE local || '@' || domain END AS key,
row_number() OVER (PARTITION BY
CASE WHEN domain IN ('gmail.com', 'googlemail.com') THEN replace(local, '.', '') || '@gmail.com'
ELSE local || '@' || domain END
ORDER BY created_at) AS n
FROM parts
)
UPDATE users u SET email_key = keyed.key FROM keyed WHERE keyed.id = u.id AND keyed.n = 1;
CREATE UNIQUE INDEX users_email_key_idx ON users (email_key);
-- +goose Down
DROP INDEX users_email_key_idx;
ALTER TABLE users DROP COLUMN email_key;
ALTER TABLE users DROP COLUMN email_verified_at;