add new account
This commit is contained in:
parent
986cec21a4
commit
ac17f86154
@ -52,10 +52,7 @@ func (s *Service) Signup(c *gin.Context) {
|
|||||||
s.Error(c, http.StatusConflict, errors.New("account exists"))
|
s.Error(c, http.StatusConflict, errors.New("account exists"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := s.DB.Create(&models.Account{
|
if err := s.DB.Create(models.NewAccount(account.Login, account.Password)).Error; err != nil {
|
||||||
Login: account.Login,
|
|
||||||
Password: account.Password,
|
|
||||||
}).Error; err != nil {
|
|
||||||
s.Error(c, http.StatusConflict, err)
|
s.Error(c, http.StatusConflict, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,9 @@ type Account struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Account) BeforeCreate(tx *gorm.DB) error {
|
func (a *Account) BeforeCreate(tx *gorm.DB) error {
|
||||||
|
if a.EncryptedPassword == "" {
|
||||||
a.EncryptPassword()
|
a.EncryptPassword()
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,3 +29,12 @@ func (a *Account) EncryptPassword() {
|
|||||||
sha1.Write([]byte(a.Password))
|
sha1.Write([]byte(a.Password))
|
||||||
a.EncryptedPassword = base64.StdEncoding.EncodeToString(sha1.Sum(nil))
|
a.EncryptedPassword = base64.StdEncoding.EncodeToString(sha1.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewAccount(login string, password string) *Account {
|
||||||
|
a := &Account{
|
||||||
|
Login: login,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
a.EncryptPassword()
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user