alai-server/backend/models/user.go

43 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-06-29 21:26:05 -04:00
package models
import (
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
type User struct {
gorm.Model
ID uint64 `json:"ID,omitempty" gorm:"primaryKey"`
Name string `json:"name,omitempty" gorm:"not null"`
Username string `json:"username,omitempty" gorm:"unique; not null"`
Email string `json:"email,omitempty" gorm:"unique;not null"`
2023-02-09 01:07:31 -03:00
Password string `json:"password,omitempty" gorm:"not null"`
NewPassword string `json:"new_password,omitempty" gorm:"-:all"`
2022-06-29 21:26:05 -04:00
}
type UserPublic struct {
User
Password bool `json:"password,omitempty"`
NewPassword bool `json:"new_password,omitempty"`
CreatedAt bool `json:"CreatedAt,omitempty"`
UpdatedAt bool `json:"UpdatedAt,omitempty"`
DeletedAt bool `json:"DeletedAt,omitempty"`
}
2022-06-29 21:26:05 -04:00
func (user *User) HashPassword(password string) error {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
return err
}
user.Password = string(bytes)
return nil
}
func (user *User) CheckPassword(providedPassword string) error {
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(providedPassword))
if err != nil {
return err
}
return nil
}