You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
982 B
44 lines
982 B
package models
|
|
|
|
import (
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// The User model represents a registered user.
|
|
type User struct {
|
|
ID int
|
|
Name string
|
|
PassHash []byte
|
|
}
|
|
|
|
// SetPassword sets the user's password
|
|
func (user *User) SetPassword(password string) error {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user.PassHash = hash
|
|
return nil
|
|
}
|
|
|
|
// CheckPassword checks the user's password against the hash.
|
|
func (user *User) CheckPassword(attempt string) error {
|
|
err := bcrypt.CompareHashAndPassword(user.PassHash, []byte(attempt))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// UserRepository is an interface for all database operations
|
|
// the user model makes.
|
|
type UserRepository interface {
|
|
FindUserByID(id int) (User, error)
|
|
FindUserByName(name string) (User, error)
|
|
ListUsers() ([]User, error)
|
|
InsertUser(user User) (int, error)
|
|
UpdateUser(user User) error
|
|
RemoveUser(user User) error
|
|
}
|