RezinaLMS-BackEnd/db/query/team_otps.sql
Yared Yemane 89c91484d1 fix: prevent reuse of team password-reset links
Consume the specific reset OTP atomically before updating the password, and reject already-used codes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-10 06:29:05 -07:00

42 lines
1.0 KiB
SQL

-- name: CreateTeamOtp :exec
INSERT INTO team_otps (
team_member_id,
sent_to,
medium,
otp_for,
otp,
expires_at
)
VALUES ($1, $2, $3, $4, $5, $6);
-- name: GetLatestTeamOtp :one
SELECT id, team_member_id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at
FROM team_otps
WHERE team_member_id = $1
ORDER BY id DESC
LIMIT 1;
-- name: GetTeamResetOtpByCode :one
SELECT id, team_member_id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at
FROM team_otps
WHERE team_member_id = $1
AND otp = $2
AND otp_for = 'reset'
ORDER BY id DESC
LIMIT 1;
-- Consume a reset OTP exactly once. Returns 0 rows if already used or missing.
-- name: MarkTeamOtpAsUsed :execrows
UPDATE team_otps
SET used = TRUE, used_at = $2
WHERE id = $1
AND used = FALSE;
-- Invalidate any other unused reset OTPs for the member after a successful reset.
-- name: InvalidateUnusedTeamResetOtps :exec
UPDATE team_otps
SET used = TRUE, used_at = CURRENT_TIMESTAMP
WHERE team_member_id = $1
AND otp_for = 'reset'
AND used = FALSE;