sabisan/internal/store/auth.go
V fac53d7b85
All checks were successful
Publish / Test, build, and push image (push) Successful in 3m38s
Refactor app and store layout
2026-05-17 00:03:50 +01:00

30 lines
1.1 KiB
Go

package store
import (
"context"
"time"
)
func (s *Store) AdminByUsername(ctx context.Context, username string) (AdminUser, error) {
var u AdminUser
err := s.db.QueryRowContext(ctx, `select id, username, password_hash from admin_users where username = ?`, username).Scan(&u.ID, &u.Username, &u.PasswordHash)
return u, err
}
func (s *Store) CreateSession(ctx context.Context, tokenHash string, userID int64, expiresAt time.Time) error {
_, err := s.db.ExecContext(ctx, `insert into sessions (token_hash, user_id, expires_at) values (?, ?, ?)`, tokenHash, userID, expiresAt)
return err
}
func (s *Store) SessionUser(ctx context.Context, tokenHash string) (AdminUser, error) {
var u AdminUser
err := s.db.QueryRowContext(ctx, `select u.id, u.username, u.password_hash from sessions s join admin_users u on u.id = s.user_id where s.token_hash = ? and s.expires_at > ?`, tokenHash, time.Now()).
Scan(&u.ID, &u.Username, &u.PasswordHash)
return u, err
}
func (s *Store) DeleteSession(ctx context.Context, tokenHash string) error {
_, err := s.db.ExecContext(ctx, `delete from sessions where token_hash = ?`, tokenHash)
return err
}