删除 .history/models/user_20240927093317.go
This commit is contained in:
parent
9118c5fc98
commit
6c07d14e78
@ -1,38 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type User struct {
|
|
||||||
ID uint `gorm:"primarykey" json:"id"`
|
|
||||||
Username string `gorm:"unique" json:"username"`
|
|
||||||
Password string `json:"-"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
Phone string `json:"phone"`
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *User) BeforeCreate(tx *gorm.DB) error {
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
u.Password = string(hashedPassword)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *User) ComparePassword(password string) error {
|
|
||||||
//直接比较密码,不用bcrypt
|
|
||||||
if u.Password == password {
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return errors.New("error password")
|
|
||||||
// bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user