英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论