add: Login system
This commit is contained in:
@@ -2,20 +2,21 @@ package models
|
||||
|
||||
import (
|
||||
"GoMembership/internal/constants"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
UpdatedAt time.Time
|
||||
DateOfBirth time.Time `gorm:"not null" json:"date_of_birth" validate:"required,age"`
|
||||
CreatedAt time.Time
|
||||
Salt *string `json:"-"`
|
||||
Company string `json:"company" validate:"omitempty,omitnil"`
|
||||
Phone string `json:"phone" validate:"omitempty,omitnil"`
|
||||
Notes *string `json:"notes"`
|
||||
FirstName string `gorm:"not null" json:"first_name" validate:"required"`
|
||||
Password string `json:"password"`
|
||||
Password string `json:"password" required_unless=RoleID 0`
|
||||
Email string `gorm:"unique;not null" json:"email" validate:"required,email"`
|
||||
LastName string `gorm:"not null" json:"last_name" validate:"required"`
|
||||
ProfilePicture string `json:"profile_picture" validate:"omitempty,omitnil,image"`
|
||||
@@ -40,3 +41,12 @@ func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (u *User) PasswordMatches(plaintextPassword string) (bool, error) {
|
||||
match, err := argon2id.ComparePasswordAndHash(plaintextPassword, u.Password)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return match, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user