| 1 | -- name: CreateRefreshToken :exec |
| 2 | INSERT INTO refresh_tokens (user_id, token_hash, family_id, parent_id, expires_at) |
| 3 | VALUES ($1, $2, $3, $4, $5); |
| 4 | |
| 5 | -- name: GetRefreshTokenForUpdate :one |
| 6 | -- Locks the token's row: two refreshes with the same token take turns. |
| 7 | SELECT * FROM refresh_tokens |
| 8 | WHERE token_hash = $1 |
| 9 | FOR UPDATE; |
| 10 | |
| 11 | -- name: RotateRefreshToken :exec |
| 12 | UPDATE refresh_tokens SET rotated_at = now() |
| 13 | WHERE id = $1; |
| 14 | |
| 15 | -- name: RevokeFamily :execrows |
| 16 | UPDATE refresh_tokens SET revoked_at = now() |
| 17 | WHERE family_id = $1 AND revoked_at IS NULL; |
| 18 | |
| 19 | -- name: RevokeFamilyOf :execrows |
| 20 | -- Revokes the family of the token with this hash: a logout. |
| 21 | UPDATE refresh_tokens SET revoked_at = now() |
| 22 | WHERE revoked_at IS NULL |
| 23 | AND family_id = (SELECT t.family_id FROM refresh_tokens t WHERE t.token_hash = $1); |