163 lines
3.7 KiB
Go
163 lines
3.7 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: auth.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
const consumeVerification = `-- name: ConsumeVerification :one
|
|
UPDATE verifications SET consumed_at = now(), updated_at = now()
|
|
WHERE value = $1 AND consumed_at IS NULL AND expires_at > now()
|
|
RETURNING identifier
|
|
`
|
|
|
|
func (q *Queries) ConsumeVerification(ctx context.Context, value string) (string, error) {
|
|
row := q.db.QueryRow(ctx, consumeVerification, value)
|
|
var identifier string
|
|
err := row.Scan(&identifier)
|
|
return identifier, err
|
|
}
|
|
|
|
const createSession = `-- name: CreateSession :exec
|
|
INSERT INTO sessions (id, token, expires_at, user_id, ip_address, user_agent)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
`
|
|
|
|
type CreateSessionParams struct {
|
|
ID string
|
|
Token string
|
|
ExpiresAt time.Time
|
|
UserID string
|
|
IpAddress *string
|
|
UserAgent *string
|
|
}
|
|
|
|
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) error {
|
|
_, err := q.db.Exec(ctx, createSession,
|
|
arg.ID,
|
|
arg.Token,
|
|
arg.ExpiresAt,
|
|
arg.UserID,
|
|
arg.IpAddress,
|
|
arg.UserAgent,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const createVerification = `-- name: CreateVerification :exec
|
|
INSERT INTO verifications (id, identifier, value, expires_at)
|
|
VALUES ($1, $2, $3, $4)
|
|
`
|
|
|
|
type CreateVerificationParams struct {
|
|
ID string
|
|
Identifier string
|
|
Value string
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
func (q *Queries) CreateVerification(ctx context.Context, arg CreateVerificationParams) error {
|
|
_, err := q.db.Exec(ctx, createVerification,
|
|
arg.ID,
|
|
arg.Identifier,
|
|
arg.Value,
|
|
arg.ExpiresAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const deleteSession = `-- name: DeleteSession :exec
|
|
DELETE FROM sessions WHERE token = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteSession(ctx context.Context, token string) error {
|
|
_, err := q.db.Exec(ctx, deleteSession, token)
|
|
return err
|
|
}
|
|
|
|
const purgeExpiredSessions = `-- name: PurgeExpiredSessions :exec
|
|
DELETE FROM sessions WHERE expires_at < now()
|
|
`
|
|
|
|
func (q *Queries) PurgeExpiredSessions(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, purgeExpiredSessions)
|
|
return err
|
|
}
|
|
|
|
const purgeExpiredVerifications = `-- name: PurgeExpiredVerifications :exec
|
|
DELETE FROM verifications WHERE expires_at < now() - interval '1 day'
|
|
`
|
|
|
|
func (q *Queries) PurgeExpiredVerifications(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, purgeExpiredVerifications)
|
|
return err
|
|
}
|
|
|
|
const upsertUser = `-- name: UpsertUser :one
|
|
|
|
INSERT INTO users (id, email, email_verified)
|
|
VALUES ($1, $2, true)
|
|
ON CONFLICT (email) DO UPDATE
|
|
SET email_verified = true, updated_at = now()
|
|
RETURNING id, email, name, is_admin
|
|
`
|
|
|
|
type UpsertUserParams struct {
|
|
ID string
|
|
Email string
|
|
}
|
|
|
|
type UpsertUserRow struct {
|
|
ID string
|
|
Email string
|
|
Name string
|
|
IsAdmin bool
|
|
}
|
|
|
|
// Sessions and magic-link tokens.
|
|
//
|
|
// Both are stored hashed: the plaintext never reaches the database, so a dump
|
|
// cannot be replayed into a login.
|
|
func (q *Queries) UpsertUser(ctx context.Context, arg UpsertUserParams) (UpsertUserRow, error) {
|
|
row := q.db.QueryRow(ctx, upsertUser, arg.ID, arg.Email)
|
|
var i UpsertUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Email,
|
|
&i.Name,
|
|
&i.IsAdmin,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const userBySessionToken = `-- name: UserBySessionToken :one
|
|
SELECT u.id, u.email, u.name, u.is_admin
|
|
FROM sessions s
|
|
JOIN users u ON u.id = s.user_id
|
|
WHERE s.token = $1 AND s.expires_at > now()
|
|
`
|
|
|
|
type UserBySessionTokenRow struct {
|
|
ID string
|
|
Email string
|
|
Name string
|
|
IsAdmin bool
|
|
}
|
|
|
|
func (q *Queries) UserBySessionToken(ctx context.Context, token string) (UserBySessionTokenRow, error) {
|
|
row := q.db.QueryRow(ctx, userBySessionToken, token)
|
|
var i UserBySessionTokenRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Email,
|
|
&i.Name,
|
|
&i.IsAdmin,
|
|
)
|
|
return i, err
|
|
}
|