package models import ( "fmt" "golang.org/x/crypto/bcrypt" ) type User struct { ID int64 `xorm:"int(11) autoincr not null unique pk"` Name string `xorm:"varchar(250)"` Username string `xorm:"varchar(250) not null"` Password string `xorm:"varchar(250) not null"` Email string `xorm:"varchar(250) not null"` Created int64 `xorm:"created"` Updated int64 `xorm:"updated"` } func (User) TableName() string { return "users" } // Hash a password func HashPassword(password string) (string, error) { bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) return string(bytes), err } // Check user credentials func CheckUserCredentials(username, password string) (User, error) { // Check if the user exists var user = User{Username: username} exists, err := x.Get(&user) if err != nil { return User{}, err } if !exists { return User{}, fmt.Errorf("User does not exist!") } // Check the users password err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) if err != nil { return User{}, err } return user, nil }