mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
Structuring examples - Pushed to iris-contrib/examples as well.
Former-commit-id: 24ee6ce233d83f0b394afc6c69b5a88243406045
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// Model is our User example model.
|
||||
type Model struct {
|
||||
ID int64 `json:"id"`
|
||||
Firstname string `json:"firstname"`
|
||||
Username string `json:"username"`
|
||||
// password is the client-given password
|
||||
// which will not be stored anywhere in the server.
|
||||
// It's here only for actions like registration and update password,
|
||||
// because we caccept a Model instance
|
||||
// inside the `DataSource#InsertOrUpdate` function.
|
||||
password string
|
||||
HashedPassword []byte `json:"-"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user