Skip to content

Commit

Permalink
fix: handle ErrNoDocuments
Browse files Browse the repository at this point in the history
  • Loading branch information
kastnerorz committed Mar 31, 2020
1 parent 62897f3 commit c66868d
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions service/auth_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c66868d

Please sign in to comment.