mirror of
https://github.com/kataras/iris.git
synced 2025-03-13 10:14:14 +01:00
42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package datamodels
|
|
|
|
import (
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// User is our User example model.
|
|
// Keep note that the tags for public-use (for our web app)
|
|
// should be kept in other file like "web/viewmodels/user.go"
|
|
// which could wrap by embedding the datamodels.User or
|
|
// define completely new fields instead but for the shake
|
|
// of the example, we will use this datamodel
|
|
// as the only one User model in our application.
|
|
type User struct {
|
|
ID int64 `json:"id" form:"id"`
|
|
Firstname string `json:"firstname" form:"firstname"`
|
|
Username string `json:"username" form:"username"`
|
|
HashedPassword []byte `json:"-" form:"-"`
|
|
CreatedAt time.Time `json:"created_at" form:"created_at"`
|
|
}
|
|
|
|
// IsValid can do some very very simple "low-level" data validations.
|
|
func (u User) IsValid() bool {
|
|
return u.ID > 0
|
|
}
|
|
|
|
// GeneratePassword will generate a hashed password for us based on the
|
|
// user's input.
|
|
func GeneratePassword(userPassword string) ([]byte, error) {
|
|
return bcrypt.GenerateFromPassword([]byte(userPassword), bcrypt.DefaultCost)
|
|
}
|
|
|
|
// ValidatePassword will check if passwords are matched.
|
|
func ValidatePassword(userPassword string, hashed []byte) (bool, error) {
|
|
if err := bcrypt.CompareHashAndPassword(hashed, []byte(userPassword)); err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|