mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
Update the _examples/mvc/login example - add a Path property at mvc.Response and add a context.Proceed helper function
Former-commit-id: b898901fe4a324e888a6e09c93530cf7a551cf2a
This commit is contained in:
41
_examples/mvc/login/datamodels/user.go
Normal file
41
_examples/mvc/login/datamodels/user.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user