feat(identity): register accounts and resend verification mail

This commit is contained in:
shira committed 2026-10-06 23:59:51 +03:00
1 parent 5e202ac972
commit 4436f626a3
55 files changed
+3227 -88

No files matched your search

+139 -4
View File
@@ -273,7 +273,10 @@ INSERT INTO email_tokens (
token_hash,
registration_version,
created_at,
expires_at
expires_at,
pending_password_hash,
pending_scenario,
pending_onboarding_step
) VALUES (
sqlc.arg(id),
sqlc.arg(user_id),
@@ -281,12 +284,92 @@ INSERT INTO email_tokens (
sqlc.arg(token_hash),
sqlc.arg(registration_version),
sqlc.arg(created_at),
sqlc.arg(expires_at)
sqlc.arg(expires_at),
sqlc.narg(pending_password_hash),
sqlc.narg(pending_scenario),
sqlc.narg(pending_onboarding_step)
);
-- The pending credentials of the newest token of a registration, for a
-- resend, which issues its token with the same credentials.
-- name: GetLatestPendingRegistration :one
SELECT
pending_password_hash,
pending_scenario,
pending_onboarding_step
FROM email_tokens
WHERE
user_id = sqlc.arg(user_id)
AND purpose = sqlc.arg(purpose)
AND registration_version = sqlc.arg(registration_version)
AND pending_password_hash IS NOT NULL
ORDER BY created_at DESC, id DESC
LIMIT 1;
-- Starts a new registration of an unverified account without touching its
-- credentials, scenario, or onboarding step: the new registration carries
-- them on its verification token. Advances the registration version, which
-- ends every earlier token, and deletes the registration C1 of the earlier
-- registrant like UpdateUserRegistration does.
-- name: AdvanceUserRegistration :one
WITH updated AS (
UPDATE users
SET
registration_version = registration_version + 1,
last_registered_at = sqlc.arg(registered_at),
updated_at = sqlc.arg(registered_at),
version = version + 1
WHERE
users.id = sqlc.arg(id)
AND users.version = sqlc.arg(expected_version)
AND users.email_verified_at IS NULL
RETURNING
users.id,
users.email,
users.email_normalized,
users.password_hash,
users.scenario,
users.onboarding_step,
users.email_verified_at,
users.registration_version,
users.last_registered_at,
users.version,
users.created_at,
users.updated_at
),
discarded_consent AS (
DELETE FROM consent_records
USING updated
WHERE
consent_records.subject_id = updated.id
AND consent_records.purpose = 'c1'
AND consent_records.source = 'registration'
)
SELECT
updated.id,
updated.email,
updated.email_normalized,
updated.password_hash,
updated.scenario,
updated.onboarding_step,
updated.email_verified_at,
updated.registration_version,
updated.last_registered_at,
updated.version,
updated.created_at,
updated.updated_at
FROM updated;
-- name: InvalidateActiveEmailTokens :execrows
UPDATE email_tokens
SET invalidated_at = sqlc.arg(invalidated_at)
SET
invalidated_at = sqlc.arg(invalidated_at),
-- An invalidated token keeps no password hash: it can never be confirmed.
pending_password_hash = NULL,
pending_scenario = NULL,
pending_onboarding_step = NULL
WHERE
user_id = sqlc.arg(user_id)
AND purpose = sqlc.arg(purpose)
@@ -323,7 +406,10 @@ RETURNING
token.created_at,
token.expires_at,
token.consumed_at,
token.invalidated_at;
token.invalidated_at,
token.pending_password_hash,
token.pending_scenario,
token.pending_onboarding_step;
-- Explains why ConsumeEmailToken matched no row.
-- name: GetEmailTokenState :one
@@ -416,3 +502,52 @@ SET revoked_at = sqlc.arg(revoked_at)
WHERE
user_id = sqlc.arg(user_id)
AND revoked_at IS NULL;
-- Stores the replay record of a command. A record whose retention has passed
-- is replaced, so the key is accepted as new after the retention. A live
-- record is left as it is and nothing is returned; the caller then reads it
-- with GetLiveIdempotencyRecord. Concurrent callers with the same scope wait
-- on the row of the first one until its transaction ends.
-- name: ReserveIdempotencyRecord :one
INSERT INTO idempotency_records (
scope_hash,
fingerprint,
created_at,
expires_at
) VALUES (
sqlc.arg(scope_hash),
sqlc.arg(fingerprint),
sqlc.arg(created_at),
sqlc.arg(expires_at)
)
ON CONFLICT (scope_hash) DO UPDATE
SET
fingerprint = excluded.fingerprint,
created_at = excluded.created_at,
expires_at = excluded.expires_at
WHERE idempotency_records.expires_at <= excluded.created_at
RETURNING scope_hash, fingerprint, created_at, expires_at;
-- name: GetLiveIdempotencyRecord :one
SELECT
scope_hash,
fingerprint,
created_at,
expires_at
FROM idempotency_records
WHERE
scope_hash = sqlc.arg(scope_hash)
AND expires_at > sqlc.arg(at);
-- Deletes at most batch_limit expired records. Locked rows are skipped, so
-- the statement never waits for a concurrent reservation.
-- name: PurgeExpiredIdempotencyRecords :execrows
DELETE FROM idempotency_records
WHERE scope_hash IN (
SELECT stale.scope_hash
FROM idempotency_records AS stale
WHERE stale.expires_at <= sqlc.arg(at)
ORDER BY stale.expires_at
LIMIT sqlc.arg(batch_limit)
FOR UPDATE SKIP LOCKED
);