瀏覽代碼

cookie ruler

Paul 6 年之前
父節點
當前提交
226dde51d7
共有 1 個檔案被更改,包括 9 行新增1 行删除
  1. 9
    1
      middleware/auth/auth.go

+ 9
- 1
middleware/auth/auth.go 查看文件

@@ -53,7 +53,15 @@ func Auth(authKey string) gin.HandlerFunc {
53 53
 
54 54
 		if mapClaims, ok := token.Claims.(jwt.MapClaims); ok {
55 55
 			if expired, ok := mapClaims[ctxRequestTokenExpired].(float64); ok {
56
-				if int64(expired) < time.Now().Unix() {
56
+				if expired == 0 {
57
+					// Only cookie is exists, check token expired. app expired by itself call logout when app exit
58
+					if _, err := ctx.Cookie(ctxRequestCookieAuthorization); err != nil {
59
+						ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, token timeout"})
60
+						return
61
+					}
62
+				}
63
+
64
+				if expired < 0 || (int64(expired) < time.Now().Unix()) {
57 65
 					ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, token timeout"})
58 66
 					return
59 67
 				}