1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: user signup

This commit is contained in:
Ferdinand Mütsch
2020-05-24 16:34:32 +02:00
parent a317dc6942
commit abfaa9d768
10 changed files with 280 additions and 58 deletions

View File

@ -1,8 +1,11 @@
package services
import (
"crypto/md5"
"encoding/hex"
"github.com/jinzhu/gorm"
"github.com/muety/wakapi/models"
uuid "github.com/satori/go.uuid"
)
type UserService struct {
@ -37,3 +40,26 @@ func (srv *UserService) GetAll() ([]*models.User, error) {
}
return users, nil
}
func (srv *UserService) CreateOrGet(signup *models.Signup) (*models.User, bool, error) {
pw := md5.Sum([]byte(signup.Password))
pwString := hex.EncodeToString(pw[:])
apiKey := uuid.NewV4().String()
u := &models.User{
ID: signup.Username,
ApiKey: apiKey,
Password: pwString,
}
result := srv.Db.FirstOrCreate(u, &models.User{ID: u.ID})
if err := result.Error; err != nil {
return nil, false, err
}
if result.RowsAffected == 1 {
return u, true, nil
}
return u, false, nil
}