Browse Source

imporove something

Paul 6 years ago
parent
commit
6f388c3542
3 changed files with 14 additions and 29 deletions
  1. 2
    4
      middleware/auth/auth.go
  2. 2
    4
      middleware/auth/optional_auth.go
  3. 10
    21
      middleware/auth/session_redis.go

+ 2
- 4
middleware/auth/auth.go View File

19
 		var tokenFromCookie, tokenFromHeader string
19
 		var tokenFromCookie, tokenFromHeader string
20
 
20
 
21
 		tokenFromCookie, err := ctx.Cookie(ctxRequestCookieAuthorization)
21
 		tokenFromCookie, err := ctx.Cookie(ctxRequestCookieAuthorization)
22
-		if err != nil {
23
-			if err == http.ErrNoCookie {
24
-				tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
25
-			}
22
+		if err == http.ErrNoCookie {
23
+			tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
26
 		}
24
 		}
27
 
25
 
28
 		if tokenFromHeader == "" {
26
 		if tokenFromHeader == "" {

+ 2
- 4
middleware/auth/optional_auth.go View File

12
 		var tokenFromCookie, tokenFromHeader string
12
 		var tokenFromCookie, tokenFromHeader string
13
 
13
 
14
 		tokenFromCookie, err := ctx.Cookie(ctxRequestCookieAuthorization)
14
 		tokenFromCookie, err := ctx.Cookie(ctxRequestCookieAuthorization)
15
-		if err != nil {
16
-			if err == http.ErrNoCookie {
17
-				tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
18
-			}
15
+		if err == http.ErrNoCookie {
16
+			tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
19
 		}
17
 		}
20
 
18
 
21
 		if tokenFromHeader == "" {
19
 		if tokenFromHeader == "" {

+ 10
- 21
middleware/auth/session_redis.go View File

12
 }
12
 }
13
 
13
 
14
 func NewRedisSessionStore(address string, password string) *RedisSessionStore {
14
 func NewRedisSessionStore(address string, password string) *RedisSessionStore {
15
-	session := &RedisSessionStore{once:&sync.Once{}}
16
-	session.once.Do(func() {
17
-		session.client = redis.NewClient(&redis.Options{
18
-			Addr:     address,
19
-			Password: password,
20
-		})
21
-
22
-		if err := session.client.Ping().Err(); err != nil {
23
-			panic(err)
24
-		}
15
+	session := &RedisSessionStore{once: &sync.Once{}}
16
+	session.client = redis.NewClient(&redis.Options{
17
+		Addr:     address,
18
+		Password: password,
25
 	})
19
 	})
26
 
20
 
21
+	if err := session.client.Ping().Err(); err != nil {
22
+		panic(err)
23
+	}
24
+
27
 	return session
25
 	return session
28
 }
26
 }
29
 
27
 
32
 }
30
 }
33
 
31
 
34
 func (rss *RedisSessionStore) IsExistsJwtToken(key string) bool {
32
 func (rss *RedisSessionStore) IsExistsJwtToken(key string) bool {
35
-	if v := rss.client.Exists(key).Val(); v != 1 {
36
-		return false
37
-	}
38
-
39
-	return true
33
+	return rss.client.Exists(key).Val() == 1
40
 }
34
 }
41
 
35
 
42
-// DeleteJwtToken delete jwt token
43
 func (rss *RedisSessionStore) DeleteJwtToken(key string) bool {
36
 func (rss *RedisSessionStore) DeleteJwtToken(key string) bool {
44
-	if v := rss.client.Del(key).Val(); v != 1 {
45
-		return false
46
-	}
47
-
48
-	return true
37
+	return rss.client.Del(key).Val() == 1
49
 }
38
 }