1 Star 2 Fork 0

api-go / iris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
user.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
1
https://gitee.com/netscript/iris.git
git@gitee.com:netscript/iris.git
netscript
iris
iris
v11.1.1

搜索帮助