DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 8990f680c5d1447ce7c0cee0e1fc50665f9b2f4d felu/internal/handlers/users.go -rw-r--r-- 1.1 KiB
8990f680Jonni Liljamo docs: update docs link 29 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * Copyright (C) 2024 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"`
}

// PostUser returns a gin handler
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")
	}
}