register an account

This commit is contained in:
celogeek 2022-02-01 09:40:27 +01:00
parent 770b5e14d0
commit 6b1d4756bc
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
4 changed files with 42 additions and 13 deletions

View File

@ -1,9 +1,11 @@
package api package api
import ( import (
"errors"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.celogeek.com/photos/api/internal/photos/models"
"gopkg.in/validator.v2" "gopkg.in/validator.v2"
) )
@ -28,22 +30,33 @@ func (s *Service) Signup(c *gin.Context) {
var account *SignupRequest var account *SignupRequest
if c.Request.ContentLength == 0 { if c.Request.ContentLength == 0 {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ s.Error(c, http.StatusBadRequest, errors.New("missing body"))
"error": "missing body",
})
return return
} }
if err := c.ShouldBindJSON(&account); err != nil { if err := c.ShouldBindJSON(&account); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ s.Error(c, http.StatusBadRequest, err)
"error": err.Error(),
})
return return
} }
if err := validator.Validate(account); err != nil { if err := validator.Validate(account); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ s.Error(c, http.StatusExpectationFailed, err)
"error": err.Error(), 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 return
} }

View File

@ -28,3 +28,9 @@ func (s *Service) Dump(o interface{}) {
s.Logger.Printf("%s", b.Bytes()) 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(),
})
}

View File

@ -2,6 +2,7 @@ package api
import ( import (
"context" "context"
"errors"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
@ -55,9 +56,7 @@ func (s *Service) SetupRoutes() {
ac.POST("/logout", s.Logout) ac.POST("/logout", s.Logout)
s.Gin.NoRoute(func(c *gin.Context) { s.Gin.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{ s.Error(c, http.StatusNotFound, errors.New("this route doesn't exists"))
"error": "this route doesn't exists",
})
}) })
} }

View File

@ -1,14 +1,25 @@
package models package models
import ( import (
"crypto"
"encoding/base64"
"time" "time"
"gorm.io/gorm"
) )
type Account struct { type Account struct {
ID uint32 `gorm:"primary_key"` ID uint32 `gorm:"primary_key"`
Login string `gorm:"unique;size:64;not null"` Login string `gorm:"unique;size:64;not null"`
Password string `gorm:"-"` Password string `gorm:"-"`
EncryptedPassword string `gorm:"size:28;not null"` EncryptedPassword string `gorm:"size:44;not null"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt 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
}