2024-10-12 20:41:30 +03:00
|
|
|
package storage
|
2024-10-11 11:38:08 +03:00
|
|
|
|
|
|
|
import (
|
2024-10-12 20:41:30 +03:00
|
|
|
"log/slog"
|
|
|
|
|
2024-10-11 11:38:08 +03:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
2024-10-12 09:55:58 +03:00
|
|
|
Uuid uuid.UUID
|
|
|
|
Email string
|
|
|
|
Hash []byte // bcrypt hash of password
|
2024-10-11 11:38:08 +03:00
|
|
|
}
|
|
|
|
|
2024-10-13 17:18:53 +03:00
|
|
|
// Fits checks whether the password is correct
|
|
|
|
func (u User) Fits(password string) bool {
|
2024-10-12 09:55:58 +03:00
|
|
|
err := bcrypt.CompareHashAndPassword(u.Hash, []byte(password))
|
2024-10-11 11:38:08 +03:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2024-10-13 17:18:53 +03:00
|
|
|
// New Creates a new User
|
|
|
|
func New(email, password string) (User, error) {
|
2024-10-11 11:38:08 +03:00
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
|
|
|
if err != nil {
|
2024-10-12 20:41:30 +03:00
|
|
|
slog.Error("error creating a new user", "error", err)
|
2024-10-11 11:38:08 +03:00
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
return User{uuid.New(), email, hash}, nil
|
2024-10-12 20:41:30 +03:00
|
|
|
}
|