mirror of
https://github.com/wisplite/raster.git
synced 2026-05-01 06:32:44 -05:00
login/logout logic, breadcrumbs, initial ui stuff
This commit is contained in:
@@ -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})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user