fix(auth): cap session cache TTL by remaining session lifetime

This commit is contained in:
kk10
2026-10-10 10:38:55 +03:00
parent af88065a1c
commit d25054843c
3 changed files with 24 additions and 23 deletions
+12 -7
View File
@@ -62,15 +62,20 @@ func (q *Queries) DeleteSession(ctx context.Context, tokenHash string) (int64, e
return result.RowsAffected(), nil
}
const getSessionUserID = `-- name: GetSessionUserID :one
SELECT user_id FROM sessions
const getActiveSession = `-- name: GetActiveSession :one
SELECT user_id, expires_at FROM sessions
WHERE token_hash = $1
AND expires_at > now()
`
func (q *Queries) GetSessionUserID(ctx context.Context, tokenHash string) (uuid.UUID, error) {
row := q.db.QueryRow(ctx, getSessionUserID, tokenHash)
var user_id uuid.UUID
err := row.Scan(&user_id)
return user_id, err
type GetActiveSessionRow struct {
UserID uuid.UUID
ExpiresAt time.Time
}
func (q *Queries) GetActiveSession(ctx context.Context, tokenHash string) (GetActiveSessionRow, error) {
row := q.db.QueryRow(ctx, getActiveSession, tokenHash)
var i GetActiveSessionRow
err := row.Scan(&i.UserID, &i.ExpiresAt)
return i, err
}