register an account
This commit is contained in:
parent
770b5e14d0
commit
6b1d4756bc
@ -1,9 +1,11 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.celogeek.com/photos/api/internal/photos/models"
|
||||
"gopkg.in/validator.v2"
|
||||
)
|
||||
|
||||
@ -28,22 +30,33 @@ func (s *Service) Signup(c *gin.Context) {
|
||||
var account *SignupRequest
|
||||
|
||||
if c.Request.ContentLength == 0 {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"error": "missing body",
|
||||
})
|
||||
s.Error(c, http.StatusBadRequest, errors.New("missing body"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&account); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
s.Error(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(account); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
s.Error(c, http.StatusExpectationFailed, err)
|
||||
return
|
||||
}
|
||||
|
||||
var accountExists int64
|
||||
if err := s.DB.Model(&models.Account{}).Where("login = ?", account.Login).Count(&accountExists).Error; err != nil {
|
||||
s.Error(c, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if accountExists > 0 {
|
||||
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 {
|
||||
s.Error(c, http.StatusConflict, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -28,3 +28,9 @@ func (s *Service) Dump(o interface{}) {
|
||||
|
||||
s.Logger.Printf("%s", b.Bytes())
|
||||
}
|
||||
|
||||
func (s *Service) Error(c *gin.Context, code int, err error) {
|
||||
c.AbortWithStatusJSON(code, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
@ -55,9 +56,7 @@ func (s *Service) SetupRoutes() {
|
||||
ac.POST("/logout", s.Logout)
|
||||
|
||||
s.Gin.NoRoute(func(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": "this route doesn't exists",
|
||||
})
|
||||
s.Error(c, http.StatusNotFound, errors.New("this route doesn't exists"))
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,25 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"encoding/base64"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
ID uint32 `gorm:"primary_key"`
|
||||
Login string `gorm:"unique;size:64;not null"`
|
||||
Password string `gorm:"-"`
|
||||
EncryptedPassword string `gorm:"size:28;not null"`
|
||||
EncryptedPassword string `gorm:"size:44;not null"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (a *Account) BeforeCreate(tx *gorm.DB) error {
|
||||
sha1 := crypto.SHA256.New()
|
||||
sha1.Write([]byte(a.Password))
|
||||
a.EncryptedPassword = base64.StdEncoding.EncodeToString(sha1.Sum(nil))
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user