ADD: added basic cli tool and updated the login modal

This commit is contained in:
hwinkel
2025-12-15 00:41:30 +01:00
parent a047d57824
commit 7ec17e1e8b
15 changed files with 199 additions and 103 deletions

View File

@@ -15,6 +15,14 @@ type LoginRequest struct {
Password string `json:"password"`
}
type RegisterRequest struct {
Email string `json:"email"`
Password string `json:"password"`
Username string `json:"username"`
}
const defaultRole = "user"
var secret = []byte("secret")
func Login(c *gin.Context, db *sql.DB) {
@@ -49,6 +57,26 @@ func Login(c *gin.Context, db *sql.DB) {
}
func Register(c *gin.Context, db *sql.DB) {
var req RegisterRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
if req.Email == "" || req.Password == "" || req.Username == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Email and password are required"})
return
}
error := user.CreateUser(db, req.Email, req.Username, req.Password, []string{defaultRole})
if error != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": error.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "User created successfully"})
}
func GenerateJWT(uuid string, email string, roles []string) (any, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user": uuid,

View File

@@ -55,6 +55,10 @@ func StartServer(cfg *config.Config) {
auth.Login(c, db) // Pass the actual DB connection instead of nil
})
router.POST("/register", func(c *gin.Context) {
auth.Register(c, db)
})
router.Run(":" + cfg.Port)
}