/*
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
package handlers
import (
"net/http"
"git.src.quest/~skye/felu-ddns/internal/db"
"github.com/gin-gonic/gin"
)
type postUserData struct {
Email string `form:"email"`
InitialPwd string `form:"initial_pwd"`
}
func PostUser() gin.HandlerFunc {
return func(c *gin.Context) {
data := &postUserData{}
if err := c.Bind(data); err != nil {
c.String(http.StatusBadRequest, "Could not bind user data")
c.Abort()
return
}
if data.Email == "" {
c.String(http.StatusBadRequest, "Email can't be empty")
c.Abort()
return
}
if len(data.InitialPwd) < 10 {
c.String(http.StatusBadRequest, "Initial password should be at least 10 chars")
c.Abort()
return
}
err := db.CreateUser(data.Email, data.InitialPwd)
if err != nil {
// FIXME: Handle better
c.String(http.StatusInternalServerError, "Something went wrong while creating a new user")
c.Abort()
return
}
c.Header("HX-Trigger", "update-user-list")
}
}