add new account

This commit is contained in:
celogeek 2022-02-01 09:44:42 +01:00
parent 986cec21a4
commit ac17f86154
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 13 additions and 5 deletions

View File

@ -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
} }

View File

@ -18,7 +18,9 @@ type Account struct {
} }
func (a *Account) BeforeCreate(tx *gorm.DB) error { func (a *Account) BeforeCreate(tx *gorm.DB) error {
a.EncryptPassword() if a.EncryptedPassword == "" {
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
}