perf(auth): 降低会话校验请求与重复查库
This commit is contained in:
@@ -26,6 +26,10 @@ func NewHandler(users *auth.UserRepository) *Handler {
|
||||
}
|
||||
|
||||
func (h *Handler) Me(c *gin.Context) {
|
||||
if cached, ok := middleware.AuthenticatedUser(c); ok {
|
||||
response.OK(c, cached)
|
||||
return
|
||||
}
|
||||
if h.users == nil {
|
||||
response.ServiceUnavailable(c, "数据库未连接")
|
||||
return
|
||||
@@ -35,7 +39,12 @@ func (h *Handler) Me(c *gin.Context) {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
return
|
||||
}
|
||||
user, err := h.users.FindByID(c.Request.Context(), userID.(uint64))
|
||||
id, ok := userID.(uint64)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "用户上下文无效")
|
||||
return
|
||||
}
|
||||
user, err := h.users.FindByID(c.Request.Context(), id)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
response.Unauthorized(c, "用户不存在")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user