From 7e26f15bed78d563119de68c94d4640024d4181e Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Sat, 25 Apr 2026 00:16:05 -0700 Subject: [PATCH] early otp expiration fix --- db/query/otp.sql | 3 ++- gen/db/otp.sql.go | 3 ++- internal/repository/otp.go | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/db/query/otp.sql b/db/query/otp.sql index 93274c4..8d181a2 100644 --- a/db/query/otp.sql +++ b/db/query/otp.sql @@ -22,7 +22,8 @@ VALUES ($1, $2, $3, $4, $5, $6); SELECT id, user_id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at FROM otps WHERE user_id = $1 -ORDER BY created_at DESC LIMIT 1; +ORDER BY id DESC +LIMIT 1; -- name: MarkOtpAsUsed :exec UPDATE otps diff --git a/gen/db/otp.sql.go b/gen/db/otp.sql.go index 1b55f0c..3a9abb7 100644 --- a/gen/db/otp.sql.go +++ b/gen/db/otp.sql.go @@ -48,7 +48,8 @@ const GetOtp = `-- name: GetOtp :one SELECT id, user_id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at FROM otps WHERE user_id = $1 -ORDER BY created_at DESC LIMIT 1 +ORDER BY id DESC +LIMIT 1 ` type GetOtpRow struct { diff --git a/internal/repository/otp.go b/internal/repository/otp.go index ebba610..37f831e 100644 --- a/internal/repository/otp.go +++ b/internal/repository/otp.go @@ -50,6 +50,10 @@ func (s *Store) GetOtp(ctx context.Context, userID int64) (domain.Otp, error) { } return domain.Otp{}, err } + if !row.ExpiresAt.Valid { + return domain.Otp{}, domain.ErrOtpNotFound + } + return domain.Otp{ ID: row.ID, UserID: row.UserID,