implemented user-adding

This commit is contained in:
z1glr
2025-01-11 16:12:02 +00:00
parent 4f203704a6
commit 059d14acc4
16 changed files with 658 additions and 334 deletions

View File

@@ -1,13 +1,36 @@
package router
import "golang.org/x/crypto/bcrypt"
import (
"github.com/gofiber/fiber/v2"
"github.com/johannesbuehl/golunteer/backend/pkg/db/users"
)
// hashes a password
func hashPassword(password string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
}
func postUser(args HandlerArgs) responseMessage {
response := responseMessage{}
// validates a password against the password-rules
func validatePassword(password string) bool {
return len(password) >= 12 && len(password) <= 64
// check admin
if !args.User.Admin {
response.Status = fiber.StatusForbidden
} else {
// parse the body
var body users.UserAdd
if err := args.C.BodyParser(&body); err != nil {
response.Status = fiber.StatusBadRequest
logger.Warn().Msgf("can't parse body: %v", err)
// validate the body
} else if err := validate.Struct(body); err != nil {
response.Status = fiber.StatusBadRequest
logger.Warn().Msgf("invalid body: %v", err)
} else if err := users.Add(body); err != nil {
response.Status = fiber.StatusInternalServerError
logger.Warn().Msgf("can't add user: %v", err)
}
}
return response
}