37 lines
748 B
Go
37 lines
748 B
Go
package handlers_auth
|
|
|
|
import (
|
|
"brunel/auth"
|
|
"brunel/domain"
|
|
"brunel/helpers"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func Register(c *fiber.Ctx) error {
|
|
|
|
username := c.FormValue("username")
|
|
password := c.FormValue("password")
|
|
passwordConfirm := c.FormValue("passwordConfirm")
|
|
|
|
_, err := helpers.DBInst.GetUser(username)
|
|
if err == nil {
|
|
return c.Status(fiber.StatusBadRequest).SendString("Username already taken")
|
|
}
|
|
|
|
if password != passwordConfirm {
|
|
return c.Status(fiber.StatusBadRequest).SendString("Passwords do not match")
|
|
}
|
|
|
|
pw := auth.NewPasswordHash(password)
|
|
|
|
user := domain.User{
|
|
Username: username,
|
|
PasswordHash: pw,
|
|
}
|
|
|
|
helpers.DBInst.CreateUser(user)
|
|
|
|
return c.Status(fiber.StatusOK).SendString("User created")
|
|
}
|