Files
spend-sparrow/db/auth.go

79 lines
1.7 KiB
Go

package db
import (
"database/sql"
"errors"
"me-fit/types"
"me-fit/utils"
"time"
"github.com/google/uuid"
)
var (
ErrUserNotFound = errors.New("User not found")
)
type User struct {
Id uuid.UUID
Email string
EmailVerified bool
EmailVerifiedAt time.Time
IsAdmin bool
Password []byte
Salt []byte
CreateAt time.Time
}
func NewUser(id uuid.UUID, email string, emailVerified bool, emailVerifiedAt time.Time, isAdmin bool, password []byte, salt []byte, createAt time.Time) *User {
return &User{
Id: id,
Email: email,
EmailVerified: emailVerified,
EmailVerifiedAt: emailVerifiedAt,
IsAdmin: isAdmin,
Password: password,
Salt: salt,
CreateAt: createAt,
}
}
type DbAuth interface {
GetUser(email string) (*User, error)
}
type DbAuthSqlite struct {
db *sql.DB
}
func NewDbAuthSqlite(db *sql.DB) *DbAuthSqlite {
return &DbAuthSqlite{db: db}
}
func (db DbAuthSqlite) GetUser(email string) (*User, error) {
var (
userId uuid.UUID
emailVerified bool
emailVerifiedAt time.Time
isAdmin bool
password []byte
salt []byte
createdAt time.Time
)
err := db.db.QueryRow(`
SELECT user_uuid, email_verified, email_verified_at, password, salt, created_at
FROM user
WHERE email = ?`, email).Scan(&userId, &emailVerified, &emailVerifiedAt, &password, &salt, &createdAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, ErrUserNotFound
} else {
utils.LogError("SQL error GetUser", err)
return nil, types.ErrInternal
}
}
return NewUser(userId, email, emailVerified, emailVerifiedAt, isAdmin, password, salt, createdAt), nil
}