fix: unify auth_handler.go response format

Standardize all JSON responses to {code: 0, message: "success", data: ...} for success
and {code: XXX, message: "..."} for errors.
This commit is contained in:
2026-04-11 13:37:39 +08:00
parent d531429674
commit c39796b70d

View File

@@ -200,32 +200,32 @@ func (h *AuthHandler) GetAuthCapabilities(c *gin.Context) {
func (h *AuthHandler) OAuthLogin(c *gin.Context) { func (h *AuthHandler) OAuthLogin(c *gin.Context) {
provider := c.Param("provider") provider := c.Param("provider")
c.JSON(http.StatusOK, gin.H{"provider": provider, "message": "OAuth not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "OAuth not configured", "data": gin.H{"provider": provider}})
} }
func (h *AuthHandler) OAuthCallback(c *gin.Context) { func (h *AuthHandler) OAuthCallback(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"error": "OAuth not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "OAuth not configured"})
} }
func (h *AuthHandler) OAuthExchange(c *gin.Context) { func (h *AuthHandler) OAuthExchange(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"error": "OAuth not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "OAuth not configured"})
} }
func (h *AuthHandler) GetEnabledOAuthProviders(c *gin.Context) { func (h *AuthHandler) GetEnabledOAuthProviders(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"providers": []string{}}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"providers": []string{}}})
} }
func (h *AuthHandler) ActivateEmail(c *gin.Context) { func (h *AuthHandler) ActivateEmail(c *gin.Context) {
token := c.Query("token") token := c.Query("token")
if token == "" { if token == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "token is required"}) c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "token is required"})
return return
} }
if err := h.authService.ActivateEmail(c.Request.Context(), token); err != nil { if err := h.authService.ActivateEmail(c.Request.Context(), token); err != nil {
handleError(c, err) handleError(c, err)
return return
} }
c.JSON(http.StatusOK, gin.H{"message": "email activated successfully"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "email activated successfully"})
} }
func (h *AuthHandler) ResendActivationEmail(c *gin.Context) { func (h *AuthHandler) ResendActivationEmail(c *gin.Context) {
@@ -233,7 +233,7 @@ func (h *AuthHandler) ResendActivationEmail(c *gin.Context) {
Email string `json:"email" binding:"required,email"` Email string `json:"email" binding:"required,email"`
} }
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return return
} }
if err := h.authService.ResendActivationEmail(c.Request.Context(), req.Email); err != nil { if err := h.authService.ResendActivationEmail(c.Request.Context(), req.Email); err != nil {
@@ -241,7 +241,7 @@ func (h *AuthHandler) ResendActivationEmail(c *gin.Context) {
return return
} }
// 防枚举:无论邮箱是否存在,统一返回成功 // 防枚举:无论邮箱是否存在,统一返回成功
c.JSON(http.StatusOK, gin.H{"message": "activation email sent if address is registered"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "activation email sent if address is registered"})
} }
func (h *AuthHandler) SendEmailCode(c *gin.Context) { func (h *AuthHandler) SendEmailCode(c *gin.Context) {
@@ -249,7 +249,7 @@ func (h *AuthHandler) SendEmailCode(c *gin.Context) {
Email string `json:"email" binding:"required,email"` Email string `json:"email" binding:"required,email"`
} }
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return return
} }
@@ -258,7 +258,7 @@ func (h *AuthHandler) SendEmailCode(c *gin.Context) {
handleError(c, err) handleError(c, err)
return return
} }
c.JSON(http.StatusOK, gin.H{"message": "验证码已发送"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "验证码已发送"})
} }
func (h *AuthHandler) LoginByEmailCode(c *gin.Context) { func (h *AuthHandler) LoginByEmailCode(c *gin.Context) {
@@ -271,7 +271,7 @@ func (h *AuthHandler) LoginByEmailCode(c *gin.Context) {
DeviceOS string `json:"device_os"` DeviceOS string `json:"device_os"`
} }
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return return
} }
@@ -311,19 +311,19 @@ func (h *AuthHandler) BootstrapAdmin(c *gin.Context) {
// P0 修复BootstrapAdmin 端点需要 bootstrap secret 验证 // P0 修复BootstrapAdmin 端点需要 bootstrap secret 验证
bootstrapSecret := os.Getenv("BOOTSTRAP_SECRET") bootstrapSecret := os.Getenv("BOOTSTRAP_SECRET")
if bootstrapSecret == "" { if bootstrapSecret == "" {
c.JSON(http.StatusForbidden, gin.H{"error": "引导初始化未授权"}) c.JSON(http.StatusForbidden, gin.H{"code": 403, "message": "引导初始化未授权"})
return return
} }
providedSecret := c.GetHeader("X-Bootstrap-Secret") providedSecret := c.GetHeader("X-Bootstrap-Secret")
if providedSecret == "" { if providedSecret == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "缺少引导密钥"}) c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "缺少引导密钥"})
return return
} }
// 使用恒定时间比较防止时序攻击 // 使用恒定时间比较防止时序攻击
if subtle.ConstantTimeCompare([]byte(providedSecret), []byte(bootstrapSecret)) != 1 { if subtle.ConstantTimeCompare([]byte(providedSecret), []byte(bootstrapSecret)) != 1 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "引导密钥无效"}) c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "引导密钥无效"})
return return
} }
@@ -334,7 +334,7 @@ func (h *AuthHandler) BootstrapAdmin(c *gin.Context) {
} }
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return return
} }
@@ -359,39 +359,39 @@ func (h *AuthHandler) BootstrapAdmin(c *gin.Context) {
} }
func (h *AuthHandler) SendEmailBindCode(c *gin.Context) { func (h *AuthHandler) SendEmailBindCode(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "email bind not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "email bind not configured"})
} }
func (h *AuthHandler) BindEmail(c *gin.Context) { func (h *AuthHandler) BindEmail(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "email bind not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "email bind not configured"})
} }
func (h *AuthHandler) UnbindEmail(c *gin.Context) { func (h *AuthHandler) UnbindEmail(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "email unbind not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "email unbind not configured"})
} }
func (h *AuthHandler) SendPhoneBindCode(c *gin.Context) { func (h *AuthHandler) SendPhoneBindCode(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "phone bind not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "phone bind not configured"})
} }
func (h *AuthHandler) BindPhone(c *gin.Context) { func (h *AuthHandler) BindPhone(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "phone bind not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "phone bind not configured"})
} }
func (h *AuthHandler) UnbindPhone(c *gin.Context) { func (h *AuthHandler) UnbindPhone(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "phone unbind not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "phone unbind not configured"})
} }
func (h *AuthHandler) GetSocialAccounts(c *gin.Context) { func (h *AuthHandler) GetSocialAccounts(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"accounts": []interface{}{}}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"accounts": []interface{}{}}})
} }
func (h *AuthHandler) BindSocialAccount(c *gin.Context) { func (h *AuthHandler) BindSocialAccount(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "social binding not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "social binding not configured"})
} }
func (h *AuthHandler) UnbindSocialAccount(c *gin.Context) { func (h *AuthHandler) UnbindSocialAccount(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "social unbinding not configured"}) c.JSON(http.StatusOK, gin.H{"code": 0, "message": "social unbinding not configured"})
} }
func (h *AuthHandler) SupportsEmailCodeLogin() bool { func (h *AuthHandler) SupportsEmailCodeLogin() bool {