diff --git a/internal/photos/api/account.go b/internal/photos/api/account.go index 4190129..70b2912 100644 --- a/internal/photos/api/account.go +++ b/internal/photos/api/account.go @@ -18,33 +18,19 @@ var ( // Model type Account struct { - ID uint32 `gorm:"primary_key" json:"-"` - Login string `gorm:"unique;size:64;not null" json:"login"` - Password string `gorm:"-" json:"-"` - EncryptedPassword string `gorm:"size:60;not null" json:"-"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"-"` -} - -func (a *Account) BeforeCreate(tx *gorm.DB) error { - if a.EncryptedPassword == "" { - a.EncryptPassword() - } - return nil -} - -func (a *Account) EncryptPassword() { - b, _ := bcrypt.GenerateFromPassword([]byte(a.Password), 12) - a.EncryptedPassword = string(b) + ID uint32 `gorm:"primary_key" json:"-"` + Login string `gorm:"unique;size:64;not null" json:"login"` + Password []byte `gorm:"type:varchar(60);not null" json:"-"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"-"` } func NewAccount(login string, password string) *Account { - a := &Account{ + p, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + return &Account{ Login: login, - Password: password, + Password: p, } - a.EncryptPassword() - return a } // Service diff --git a/internal/photos/api/session.go b/internal/photos/api/session.go index 634e944..1382af5 100644 --- a/internal/photos/api/session.go +++ b/internal/photos/api/session.go @@ -39,14 +39,14 @@ func (s *Session) BeforeCreate(tx *gorm.DB) error { } func NewSession(tx *gorm.DB, login string, password string) (*Session, error) { - account := &Account{Login: login} + account := &Account{} if err := tx.Where( "login = ?", - account.Login, + login, ).First(account).Error; err != nil { return nil, err } - if err := bcrypt.CompareHashAndPassword([]byte(account.EncryptedPassword), []byte(password)); err != nil { + if err := bcrypt.CompareHashAndPassword(account.Password, []byte(password)); err != nil { return nil, err }