Compare commits
3 Commits
4718dba626
...
244c701519
| Author | SHA1 | Date | |
|---|---|---|---|
| 244c701519 | |||
| 6ffe946c24 | |||
| b7b872f70b |
38
.history/models/user_20240927093111.go
Normal file
38
.history/models/user_20240927093111.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
38
.history/models/user_20240927093317.go
Normal file
38
.history/models/user_20240927093317.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
38
.history/models/user_20240927093319.go
Normal file
38
.history/models/user_20240927093319.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
@ -31,6 +32,7 @@ func (u *User) ComparePassword(password string) error {
|
|||||||
if u.Password == password {
|
if u.Password == password {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
return errors.New("error password")
|
||||||
|
// bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user