login/logout logic, breadcrumbs, initial ui stuff

This commit is contained in:
wisplite
2025-11-19 15:38:21 -06:00
parent ccf644acef
commit d176d6d761
23 changed files with 1359 additions and 36 deletions
+50 -6
View File
@@ -10,23 +10,67 @@ import (
func RegisterUserRoutes(rg *gin.RouterGroup) {
user := rg.Group("/user")
user.POST("/createUser", func(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
err := services.CreateUser(username, password)
var request struct {
Username string `json:"username"`
Password string `json:"password"`
}
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err := services.CreateUser(request.Username, request.Password)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "User created successfully"})
})
user.POST("/setRootUser", func(c *gin.Context) {
var request struct {
Username string `json:"username"`
}
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err := services.SetRootUser(request.Username)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Root user set successfully"})
})
user.POST("/login", func(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
accessToken, err := services.Login(username, password)
var request struct {
Username string `json:"username"`
Password string `json:"password"`
}
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
accessToken, err := services.Login(request.Username, request.Password)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "accessToken": accessToken.Token})
})
user.GET("/rootUserExists", func(c *gin.Context) {
exists := services.RootUserExists()
c.JSON(http.StatusOK, gin.H{"exists": exists})
})
user.GET("/getUserData", func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
token := authHeader
if len(authHeader) > 7 && authHeader[:7] == "Bearer " {
token = authHeader[7:]
}
userData, err := services.GetUserData(token)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"userData": userData})
})
}