2022-08-25 19:26:29 -06:00
|
|
|
package models
|
|
|
|
|
2022-09-11 21:59:13 -06:00
|
|
|
import "golang.org/x/crypto/bcrypt"
|
|
|
|
|
2022-08-25 19:26:29 -06:00
|
|
|
type User struct {
|
2022-08-30 20:03:24 -06:00
|
|
|
ModelBase
|
2022-09-11 21:59:13 -06:00
|
|
|
Name string `json:"name"`
|
2022-08-27 09:39:55 -06:00
|
|
|
Username string `json:"username" gorm:"unique"`
|
2022-09-11 21:59:13 -06:00
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HashPassword(password string) error {
|
|
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Password = string(bytes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (user *User) CheckPassword(providedPassword string) error {
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(providedPassword))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-08-25 19:26:29 -06:00
|
|
|
}
|