-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
63 lines (58 loc) · 1.42 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package middleware
import (
"fmt"
"github.com/GearFramework/emarket/internal/models"
"github.com/GearFramework/emarket/internal/pkg/alog"
"github.com/gin-gonic/gin"
"time"
)
const (
CookieParamName = "Authorization"
TokenExpired = time.Hour * 24
)
var logger *alog.Alog
func Auth(a models.Identifier) gin.HandlerFunc {
logger = alog.NewLogger("IdentityMiddleware")
return func(ctx *gin.Context) {
cookie, err := ctx.Request.Cookie(CookieParamName)
a.IdentityByCookie(cookie)
if err != nil || cookie == nil || cookie.Value == "" {
}
fmt.Println(err)
//if token == "" {
// logger.Warnf("%s request: %s; status: %d; size: %d; Empty authorization token",
// ctx.Request.Method,
// ctx.Request.RequestURI,
// ctx.Writer.Status(),
// ctx.Writer.Size(),
// )
// //UnauthorizedResponse.Send(ctx)
// return
//}
ctx.Next()
}
}
//
//func authFromToken(ctx *gin.Context, token string) (string, error) {
// userID, err := s.api.Auth(token)
// if err != nil && err == auth.ErrInvalidAuthorization {
// ctx.AbortWithStatus(http.StatusUnauthorized)
// return 0, err
// }
// if err != nil && err == auth.ErrNeedAuthorization {
// return s.AuthNewUser(ctx)
// }
// s.setAuthCookie(ctx, token)
// return userID, err
//}
//
//func setAuthCookie(ctx *gin.Context, token string) {
// ctx.SetCookie(CookieParamName,
// token,
// int(TokenExpired.Seconds()),
// "/",
// "localhost",
// true,
// true,
// )
//}