Uses uuid to avoid easy inferral of user ids
This commit is contained in:
parent
800c839b53
commit
932edb8da7
5 changed files with 23 additions and 12 deletions
|
|
@ -4,27 +4,36 @@ import (
|
|||
"freed/internal/model"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/log"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var defaultUserError = fiber.NewError(fiber.ErrInternalServerError.Code, "Could not create user")
|
||||
|
||||
func (h *Handler) createUser(c *fiber.Ctx) error {
|
||||
user := new(model.User)
|
||||
|
||||
if err := c.BodyParser(user); err != nil {
|
||||
return err
|
||||
userId, idErr := uuid.NewRandom()
|
||||
|
||||
if idErr != nil {
|
||||
log.Warn(idErr)
|
||||
return defaultUserError
|
||||
}
|
||||
|
||||
result, err := h.db.Exec("INSERT INTO user (first_name, email) VALUES (?, ?)", user.FirstName, user.Email)
|
||||
user.ID = userId.String()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
if parseErr := c.BodyParser(user); parseErr != nil {
|
||||
log.Warn(parseErr)
|
||||
return defaultUserError
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
_, insertErr := h.db.Exec("INSERT INTO user (id, first_name, email) VALUES (?, ?, ?)", user.ID, user.FirstName, user.Email)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
if insertErr != nil {
|
||||
log.Warn(insertErr)
|
||||
return defaultUserError
|
||||
}
|
||||
|
||||
c.SendStatus(201)
|
||||
return c.JSON(&fiber.Map{"userId": id})
|
||||
return c.JSON(&fiber.Map{"userId": userId})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue