Files
backend/internal/middleware/verification.go

42 lines
997 B
Go
Raw Normal View History

package middleware
import (
"net/http"
"with_you/internal/model"
"with_you/internal/pkg/response"
"with_you/internal/service"
"github.com/gin-gonic/gin"
)
// RequireVerified 要求用户已通过身份认证。
// 通过 service.UserService 查询用户(而非直接访问 repository保持分层一致
//
// middleware → service → repository
func RequireVerified(userService service.UserService) gin.HandlerFunc {
return func(c *gin.Context) {
userID, exists := c.Get("user_id")
if !exists {
response.Unauthorized(c, "未授权")
c.Abort()
return
}
user, err := userService.GetUserByID(c.Request.Context(), userID.(string))
if err != nil {
response.InternalServerError(c, "获取用户信息失败")
c.Abort()
return
}
if user.VerificationStatus != model.VerificationStatusApproved {
response.ErrorWithStringCode(c, http.StatusForbidden, "VERIFICATION_REQUIRED", "请先完成身份认证")
c.Abort()
return
}
c.Next()
}
}