30 lines
876 B
Go
30 lines
876 B
Go
package auth
|
|
|
|
import (
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type User struct {
|
|
ID string `gorm:"primaryKey" json:"id"`
|
|
Username string `gorm:"uniqueIndex;not null" json:"username"`
|
|
Email string `gorm:"uniqueIndex;not null" json:"email"`
|
|
Password string `gorm:"not null" json:"-"` // 不在 JSON 中返回
|
|
Role string `gorm:"default:'user'" json:"role"` // user, admin
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
}
|
|
|
|
// HashPassword 加密密码
|
|
func HashPassword(password string) (string, error) {
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
return string(bytes), err
|
|
}
|
|
|
|
// CheckPassword 验证密码
|
|
func (u *User) CheckPassword(password string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
|
return err == nil
|
|
}
|