在middleware
您必须构建一个结构对象,传递ctx.Writer
给它并设置一个指针ctx.Request.Context
并设置一个方法来为您设置cookie。
type CookieAccess struct {
Writer http.ResponseWriter
UserId uint64
IsLoggedIn bool
}
// method to write cookie
func (this *CookieAccess) SetToken(token string) {
http.SetCookie(this.Writer, &http.Cookie{
Name: cookieName,
Value: token,
HttpOnly: true,
Path: "/",
Expires: time.Now().Add(token_expire),
})
}
在你的middleware
:
func extractUserId(ctx *gin.Context) (uint64, error) {
c, err := ctx.Request.Cookie(cookieName)
if err != nil {
return 0, errors.New("There is no token in cookies")
}
userId, err := ParseToken(c.Value)
if err != nil {
return 0, err
}
return userId, nil
}
func setValInCtx(ctx *gin.Context, val interface{}) {
newCtx := context.WithValue(ctx.Request.Context(), cookieAccessKeyCtx, val)
ctx.Request = ctx.Request.WithContext(newCtx)
}
func Middleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
cookieA := CookieAccess{
Writer: ctx.Writer,
}
// &cookieA is a pointer so any changes in future is changing cookieA is context
setValInCtx(ctx, &cookieA)
userId, err := extractUserId(ctx)
if err != nil {
cookieA.IsLoggedIn = false
ctx.Next()
return
}
cookieA.UserId = userId
cookieA.IsLoggedIn = true
// calling the actual resolver
ctx.Next()
// here will execute after resolver and all other middlewares was called
// so &cookieA is safe from garbage collector
}
}
你必须在你的解析器中调用这个函数。它来来去ctx
去&cookieA
func GetCookieAccess(ctx context.Context) *CookieAccess {
return ctx.Value(cookieAccessKeyCtx).(*CookieAccess)
}
最后在你的Login
解析器中:
CA := security.GetCookieAccess(ctx)
CA.SetToken(token)
CA.UserId = userId
我希望这会对某人有所帮助:)))