[feature/backend] registration control
This commit is contained in:
parent
86ab334bc9
commit
a853374009
5 changed files with 142 additions and 49 deletions
|
@ -27,16 +27,41 @@ type AuthResponse struct {
|
|||
}
|
||||
|
||||
func (h *Handler) Register(c *gin.Context) {
|
||||
// 检查是否启用注册功能
|
||||
if !h.config.Auth.Registration.Enabled {
|
||||
message := h.config.Auth.Registration.Message
|
||||
if message == "" {
|
||||
message = "Registration is currently disabled"
|
||||
}
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": gin.H{
|
||||
"code": "REGISTRATION_DISABLED",
|
||||
"message": message,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var req RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": gin.H{
|
||||
"code": "INVALID_REQUEST",
|
||||
"message": err.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.service.CreateUser(c.Request.Context(), req.Username, req.Email, req.Password, req.Role)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to create user")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user"})
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": gin.H{
|
||||
"code": "CREATE_USER_FAILED",
|
||||
"message": "Failed to create user",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -44,7 +69,12 @@ func (h *Handler) Register(c *gin.Context) {
|
|||
roles, err := h.service.GetUserRoles(c.Request.Context(), user.ID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get user roles")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user roles"})
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": gin.H{
|
||||
"code": "GET_ROLES_FAILED",
|
||||
"message": "Failed to get user roles",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -64,7 +94,12 @@ func (h *Handler) Register(c *gin.Context) {
|
|||
tokenString, err := token.SignedString([]byte(h.cfg.JWT.Secret))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to generate token")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate token"})
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": gin.H{
|
||||
"code": "GENERATE_TOKEN_FAILED",
|
||||
"message": "Failed to generate token",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -74,14 +109,22 @@ func (h *Handler) Register(c *gin.Context) {
|
|||
func (h *Handler) Login(c *gin.Context) {
|
||||
var req LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": gin.H{
|
||||
"code": "INVALID_REQUEST",
|
||||
"message": err.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.service.GetUserByUsername(c.Request.Context(), req.Username)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"error": "Invalid username or password",
|
||||
"error": gin.H{
|
||||
"code": "INVALID_CREDENTIALS",
|
||||
"message": "Invalid username or password",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
@ -90,7 +133,10 @@ func (h *Handler) Login(c *gin.Context) {
|
|||
err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(req.Password))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"error": "Invalid username or password",
|
||||
"error": gin.H{
|
||||
"code": "INVALID_CREDENTIALS",
|
||||
"message": "Invalid username or password",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
@ -100,7 +146,10 @@ func (h *Handler) Login(c *gin.Context) {
|
|||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get user roles")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Failed to get user roles",
|
||||
"error": gin.H{
|
||||
"code": "GET_ROLES_FAILED",
|
||||
"message": "Failed to get user roles",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
@ -122,7 +171,10 @@ func (h *Handler) Login(c *gin.Context) {
|
|||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to generate token")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Failed to generate token",
|
||||
"error": gin.H{
|
||||
"code": "GENERATE_TOKEN_FAILED",
|
||||
"message": "Failed to generate token",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue