102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type Repository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewRepository(pool *pgxpool.Pool) *Repository {
|
|
return &Repository{pool: pool}
|
|
}
|
|
|
|
func (r *Repository) CreateUser(ctx context.Context, user *User) error {
|
|
user.ID = uuid.New().String()
|
|
user.CreatedAt = time.Now().UTC()
|
|
_, err := r.pool.Exec(ctx,
|
|
`INSERT INTO users (id, username, email, password_hash, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
|
user.ID, user.Username, user.Email, user.PasswordHash, user.CreatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) FindByEmail(ctx context.Context, email string) (*User, error) {
|
|
var user User
|
|
err := r.pool.QueryRow(ctx,
|
|
`SELECT id, username, email, password_hash, created_at FROM users WHERE email = $1`, email,
|
|
).Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.CreatedAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *Repository) FindByID(ctx context.Context, id string) (*User, error) {
|
|
var user User
|
|
err := r.pool.QueryRow(ctx,
|
|
`SELECT id, username, email, password_hash, created_at FROM users WHERE id = $1`, id,
|
|
).Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.CreatedAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *Repository) CreateRefreshToken(ctx context.Context, doc *RefreshTokenDoc) error {
|
|
doc.ID = uuid.New().String()
|
|
doc.CreatedAt = time.Now().UTC()
|
|
_, err := r.pool.Exec(ctx,
|
|
`INSERT INTO refresh_tokens (id, user_id, token_hash, expires_at, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
|
doc.ID, doc.UserID, doc.TokenHash, doc.ExpiresAt, doc.CreatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) FindRefreshTokenByHash(ctx context.Context, hash string) (*RefreshTokenDoc, error) {
|
|
var doc RefreshTokenDoc
|
|
err := r.pool.QueryRow(ctx,
|
|
`SELECT id, user_id, token_hash, expires_at, created_at FROM refresh_tokens WHERE token_hash = $1 AND expires_at > NOW()`, hash,
|
|
).Scan(&doc.ID, &doc.UserID, &doc.TokenHash, &doc.ExpiresAt, &doc.CreatedAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &doc, nil
|
|
}
|
|
|
|
func (r *Repository) DeleteRefreshToken(ctx context.Context, id string) error {
|
|
_, err := r.pool.Exec(ctx, `DELETE FROM refresh_tokens WHERE id = $1`, id)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) UpdateUserUsername(ctx context.Context, id, username string) error {
|
|
_, err := r.pool.Exec(ctx, `UPDATE users SET username = $1 WHERE id = $2`, username, id)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) UpdateUserPassword(ctx context.Context, id, passwordHash string) error {
|
|
_, err := r.pool.Exec(ctx, `UPDATE users SET password_hash = $1 WHERE id = $2`, passwordHash, id)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) DeleteExpiredRefreshTokens(ctx context.Context) error {
|
|
_, err := r.pool.Exec(ctx, `DELETE FROM refresh_tokens WHERE expires_at <= NOW()`)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) DeleteRefreshTokenByHash(ctx context.Context, hash string) (bool, error) {
|
|
tag, err := r.pool.Exec(ctx, `DELETE FROM refresh_tokens WHERE token_hash = $1`, hash)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return tag.RowsAffected() > 0, nil
|
|
}
|
|
|
|
var ErrNoRows = pgx.ErrNoRows
|