Refactor everything again

This commit is contained in:
Andrew-71 2024-10-13 21:03:44 +03:00
parent 7fdb0bf0f4
commit 96c369e4b1
14 changed files with 72 additions and 53 deletions

View file

@ -0,0 +1,30 @@
package user
import (
"log/slog"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)
type User struct {
Uuid uuid.UUID
Email string
Hash []byte // bcrypt hash of password
}
// Fits checks whether the password is correct
func (u User) Fits(password string) bool {
err := bcrypt.CompareHashAndPassword(u.Hash, []byte(password))
return err == nil
}
// New Creates a new User
func New(email, password string) (User, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
slog.Error("error creating a new user", "error", err)
return User{}, err
}
return User{uuid.New(), email, hash}, nil
}