From c66868d8b004382eda8b115bd73fb4d97f62b691 Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 31 Mar 2020 17:23:50 +0800 Subject: [PATCH] fix: handle ErrNoDocuments --- service/auth_middleware.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/service/auth_middleware.go b/service/auth_middleware.go index 074fb9c..b585f78 100644 --- a/service/auth_middleware.go +++ b/service/auth_middleware.go @@ -6,6 +6,7 @@ import ( "github.com/appleboy/gin-jwt/v2" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" "log" "net/http" "time" @@ -35,7 +36,7 @@ func AuthMiddleware() *jwt.GinJWTMiddleware { var user User err := collection.FindOne(mongoCtx, bson.M{"username": claims[IdentityKey].(string)}).Decode(&user) - if err != nil { + if err != nil && err != mongo.ErrNoDocuments { c.JSON(http.StatusInternalServerError, gin.H{"code": -1, "msg": "用户获取失败."}) log.Println(err) return nil @@ -55,7 +56,7 @@ func AuthMiddleware() *jwt.GinJWTMiddleware { var res User err := collection.FindOne(mongoCtx, bson.M{"username": credentials.Username}).Decode(&res) - if err != nil { + if err != nil && err != mongo.ErrNoDocuments { c.JSON(http.StatusInternalServerError, gin.H{"code": -1, "msg": "用户获取失败."}) log.Println(err) return nil, jwt.ErrFailedAuthentication