forked from mtusi2026/mtusi2026lab2
fix(auth): cap session cache TTL by remaining session lifetime
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user