2022-08-25 19:26:29 -06:00
|
|
|
package models
|
|
|
|
|
2022-12-03 13:48:35 -07:00
|
|
|
import (
|
2023-07-08 08:43:30 -06:00
|
|
|
"fmt"
|
2022-12-03 13:48:35 -07:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2023-07-08 08:43:30 -06:00
|
|
|
"strings"
|
2022-12-03 13:48:35 -07:00
|
|
|
)
|
2022-12-02 20:40:58 -07:00
|
|
|
|
2022-08-25 19:26:29 -06:00
|
|
|
type User struct {
|
2022-12-02 20:40:58 -07:00
|
|
|
ModelBase
|
2022-11-03 23:24:30 -06:00
|
|
|
FullName string `json:"full_name"`
|
2022-12-02 20:40:58 -07:00
|
|
|
Username string `json:"username" gorm:"unique"`
|
2022-09-11 21:59:13 -06:00
|
|
|
Password string `json:"password"`
|
2022-12-03 13:48:35 -07:00
|
|
|
|
|
|
|
//additional optional metadata that Fasten stores with users
|
|
|
|
Picture string `json:"picture"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
//Roles datatypes.JSON `json:"roles"`
|
2022-09-11 21:59:13 -06:00
|
|
|
}
|
2022-12-02 20:40:58 -07:00
|
|
|
|
|
|
|
func (user *User) HashPassword(password string) error {
|
2023-07-08 08:43:30 -06:00
|
|
|
if len(strings.TrimSpace(password)) == 0 {
|
|
|
|
return fmt.Errorf("password cannot be empty")
|
|
|
|
}
|
2022-12-02 20:40:58 -07:00
|
|
|
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
|
|
|
|
}
|