pye/user.go

25 lines
515 B
Go
Raw Normal View History

2024-10-11 11:38:08 +03:00
package main
import (
"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
}
func (u User) PasswordFits(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
}
func NewUser(email, password string) (User, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
return User{}, err
}
return User{uuid.New(), email, hash}, nil
2024-10-12 18:39:38 +03:00
}