Compare commits

..

3 Commits

Author SHA1 Message Date
244c701519 Merge branch 'main' of https://git.disbaidu.com/maxwell/testfb 2024-09-27 10:42:25 +08:00
6ffe946c24 update 2024-09-27 09:33:25 +08:00
b7b872f70b update 2024-09-27 09:33:09 +08:00
4 changed files with 117 additions and 1 deletions

View 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))
}
}

View 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))
}
}

View 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))
}
}

View File

@ -1,6 +1,7 @@
package models
import (
"errors"
"time"
"golang.org/x/crypto/bcrypt"
@ -31,6 +32,7 @@ func (u *User) ComparePassword(password string) error {
if u.Password == password {
return nil
} else {
return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
return errors.New("error password")
// bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
}
}