implemented user-adding
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user