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"))
|
||||
return
|
||||
}
|
||||
if err := s.DB.Create(&models.Account{
|
||||
Login: account.Login,
|
||||
Password: account.Password,
|
||||
}).Error; err != nil {
|
||||
if err := s.DB.Create(models.NewAccount(account.Login, account.Password)).Error; err != nil {
|
||||
s.Error(c, http.StatusConflict, err)
|
||||
return
|
||||
}
|
||||
|
@ -18,7 +18,9 @@ type Account struct {
|
||||
}
|
||||
|
||||
func (a *Account) BeforeCreate(tx *gorm.DB) error {
|
||||
if a.EncryptedPassword == "" {
|
||||
a.EncryptPassword()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -27,3 +29,12 @@ func (a *Account) EncryptPassword() {
|
||||
sha1.Write([]byte(a.Password))
|
||||
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