无法通过gin-contrib/sessions保存值(可能值太大)

huangapple go评论80阅读模式
英文:

Can't save value by gin-contrib/sessions (maybe value is too large)

问题

我无法在会话中保存RefreshToken。

在下面的代码中,refreshToken是一个常量,但实际上它是由AWS Cognito创建的。

有没有什么解决方法?

谢谢

import(
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func main() {
  router := gin.Default()
  store := cookie.NewStore([]byte("secret"))
  router.Use(sessions.Sessions("mysession", store))

  router.GET("ping", func(c *gin.Context) {
    session := sessions.Default(c)
    
    accessToken = "dfasdfasdf..." //长度为1086
    session.Set("at", accessToken)
    refreshToken = "dfasdfasdf..." //长度为1802
    session.Set("at", refreshToken)
    
    c.JSON(http.StatusOK, "pong")
  })

  router.GET("ping2", func(c *gin.Context) {
    session := sessions.Default(c)
    
	session := sessions.Default(c)
	c.JSON(http.StatusCreated, gin.H{
		"at": session.Get("at"),
		"rt": session.Get("rt"),
	})
    
    c.JSON(http.StatusOK, "pong")
  })
  router.Run(":8080")
}
英文:

I cannot save RefreshToken on session.

In the code below, refreshToken is constant, but Actually it is created by AWS Cognito.

Is there any workaround??

thank you

import(
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func main() {
  router := gin.Default()
  store := cookie.NewStore([]byte("secret"))
  router.Use(sessions.Sessions("mysession", store))

  router.GET("ping", func(c *gin.Context) {
    session := sessions.Default(c)
    
    accessToken = "dfasdfasdf..."() //length is 1086
    session.Set("at", accessToken)
    refreshToken = "dfasdfasdf..."() //length is 1802
    session.Set("at", refreshToken)
    
    c.JSON(http.StatusOK, "pong")
  })

  router.GET("ping2", func(c *gin.Context) {
    session := sessions.Default(c)
    
	session := sessions.Default(c)
	c.JSON(http.StatusCreated, gin.H{
		"at": session.Get("at"),
		"rt": session.Get("rt"),
	})
    
    c.JSON(http.StatusOK, "pong")
  })
  router.Run(":8080")
}

答案1

得分: 0

在使用session.Set之后,你需要调用session.Save()

例如:

session.Set("accessToken", accessToken)
session.Set("refreshToken", refreshToken)
session.Save()
英文:

you need to call session.Save() after using session.Set.

e.g.

session.Set("accessToken", accessToken)
session.Set("refreshToken", refreshToken)
session.Save()

huangapple
  • 本文由 发表于 2022年10月11日 17:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/74026074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定