40 lines
1.3 KiB
Go
40 lines
1.3 KiB
Go
// models/user.go
|
|
package models
|
|
|
|
import (
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type User struct {
|
|
ID uint `json:"id" gorm:"primaryKey"`
|
|
Email string `json:"email" gorm:"uniqueIndex;not null"`
|
|
Password string `json:"-" gorm:"not null"`
|
|
FirstName string `json:"first_name" gorm:"not null"`
|
|
LastName string `json:"last_name" gorm:"not null"`
|
|
Phone string `json:"phone"`
|
|
Experience string `json:"experience"`
|
|
Goals string `json:"goals"`
|
|
Newsletter bool `json:"newsletter"`
|
|
Role string `json:"role" gorm:"default:user"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
|
}
|
|
|
|
// HashPassword хеширует пароль перед сохранением
|
|
func (u *User) HashPassword() error {
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
u.Password = string(hashedPassword)
|
|
return nil
|
|
}
|
|
|
|
// CheckPassword проверяет пароль
|
|
func (u *User) CheckPassword(password string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
|
return err == nil
|
|
} |