2024-10-25 23:31:57 +02:00
|
|
|
// Code generated by sqlc. DO NOT EDIT.
|
|
|
|
// versions:
|
|
|
|
// sqlc v1.27.0
|
|
|
|
// source: refresh_token.sql
|
|
|
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
const createRefreshToken = `-- name: CreateRefreshToken :one
|
|
|
|
INSERT INTO refresh_tokens (token, created_at, updated_at, user_id, expires_at, revoked_at)
|
|
|
|
VALUES (
|
|
|
|
$1,
|
|
|
|
NOW(),
|
|
|
|
NOW(),
|
|
|
|
$2,
|
|
|
|
$3,
|
|
|
|
NULL
|
|
|
|
)
|
|
|
|
RETURNING token, created_at, updated_at, user_id, expires_at, revoked_at
|
|
|
|
`
|
|
|
|
|
|
|
|
type CreateRefreshTokenParams struct {
|
|
|
|
Token string
|
|
|
|
UserID uuid.UUID
|
|
|
|
ExpiresAt sql.NullTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CreateRefreshToken(ctx context.Context, arg CreateRefreshTokenParams) (RefreshToken, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, createRefreshToken, arg.Token, arg.UserID, arg.ExpiresAt)
|
|
|
|
var i RefreshToken
|
|
|
|
err := row.Scan(
|
|
|
|
&i.Token,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.UserID,
|
|
|
|
&i.ExpiresAt,
|
|
|
|
&i.RevokedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
2024-10-26 21:19:11 +02:00
|
|
|
|
|
|
|
const getUserFromRefreshToken = `-- name: GetUserFromRefreshToken :one
|
|
|
|
SELECT user_id FROM refresh_tokens
|
|
|
|
WHERE refresh_tokens.token = $1
|
|
|
|
AND refresh_tokens.expires_at > NOW()
|
|
|
|
AND refresh_tokens.revoked_at IS NULL
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetUserFromRefreshToken(ctx context.Context, token string) (uuid.UUID, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getUserFromRefreshToken, token)
|
|
|
|
var user_id uuid.UUID
|
|
|
|
err := row.Scan(&user_id)
|
|
|
|
return user_id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const revokeRefreshToken = `-- name: RevokeRefreshToken :exec
|
|
|
|
UPDATE refresh_tokens
|
|
|
|
SET
|
|
|
|
revoked_at = NOW(),
|
|
|
|
updated_at = NOW()
|
|
|
|
WHERE token = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) RevokeRefreshToken(ctx context.Context, token string) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, revokeRefreshToken, token)
|
|
|
|
return err
|
|
|
|
}
|