This commit is contained in:
Tomas Dvorak
2025-10-23 22:26:50 +02:00
parent 63700eedb2
commit 70ea0c3c91
75 changed files with 3337 additions and 1160 deletions
+37 -4
View File
@@ -109,7 +109,7 @@ func (ac *AuthController) Register(c *gin.Context) {
// Check if this is the first user (admin)
var userCount int64
ac.DB.Model(&models.User{}).Count(&userCount)
role := "editor"
role := "fan"
isFirstUser := userCount == 0
if isFirstUser {
role = "admin"
@@ -287,6 +287,39 @@ func (ac *AuthController) GetCurrentUser(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"user": ac.toUserModel(user.(*models.User))})
}
// UpdateCurrentUser allows the authenticated user to update their personal information
func (ac *AuthController) UpdateCurrentUser(c *gin.Context) {
u, exists := c.Get("user")
if !exists || u == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "User not authenticated"})
return
}
current := u.(*models.User)
var req struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fn := strings.TrimSpace(req.FirstName)
ln := strings.TrimSpace(req.LastName)
if fn != "" {
current.FirstName = fn
}
if ln != "" {
current.LastName = ln
}
if err := ac.DB.Save(current).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update profile"})
return
}
c.JSON(http.StatusOK, gin.H{"user": ac.toUserModel(current)})
}
// AdminExists returns whether any admin user exists
func (ac *AuthController) AdminExists(c *gin.Context) {
var count int64
@@ -435,8 +468,8 @@ func (ac *AuthController) AdminCreateUser(c *gin.Context) {
return
}
// role
role := req.Role
if role != "admin" && role != "editor" {
role := strings.TrimSpace(req.Role)
if role != "admin" && role != "editor" && role != "fan" {
role = "editor"
}
// active
@@ -527,7 +560,7 @@ func (ac *AuthController) AdminUpdateUser(c *gin.Context) {
user.Email = email
}
if req.Role != "" {
if req.Role != "admin" && req.Role != "editor" {
if req.Role != "admin" && req.Role != "editor" && req.Role != "fan" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid role"})
return
}