2021-11-22 08:05:57 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
"regexp"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type User struct {
|
2021-11-25 06:37:30 +00:00
|
|
|
Name string
|
|
|
|
|
Password string
|
|
|
|
|
Hash []byte
|
|
|
|
|
Homepage string
|
|
|
|
|
About string
|
2021-11-26 22:36:48 +00:00
|
|
|
Style string
|
|
|
|
|
Picture string
|
2021-11-28 14:31:51 +00:00
|
|
|
Email string
|
2021-11-25 06:37:30 +00:00
|
|
|
CreatedAt time.Time
|
2021-11-22 08:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u User) Validate() error {
|
|
|
|
|
if u.Name == "" {
|
|
|
|
|
return errors.New("username is mandatory")
|
|
|
|
|
}
|
|
|
|
|
if u.Password == "" {
|
|
|
|
|
return errors.New("password is mandatory")
|
|
|
|
|
}
|
|
|
|
|
match, _ := regexp.MatchString("^[a-z0-9-_]+$", u.Name)
|
|
|
|
|
if !match {
|
|
|
|
|
return errors.New("username should match [a-z0-9-_]")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u User) HashPassword() ([]byte, error) {
|
|
|
|
|
return bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.MinCost)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u User) CompareHashToPassword(hash []byte) error {
|
|
|
|
|
return bcrypt.CompareHashAndPassword(hash, []byte(u.Password))
|
|
|
|
|
}
|