Utilizing the latest version of Next.js v14.2.3 and App Router.
I am currently implementing cookie-based sessions from the gin-contrib documentation, in order to increase a session count.
// Backend Golang code snippet
...
cookieStore := sessions.NewCookieStore([]byte("secret"))
router.Use(
sessions.Sessions("mysession", cookieStore),
)
router.GET("incr", func(c *gin.Context) {
session := sessions.Default(c)
c.Header("Content-Type", "application/json")
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
...
Testing with Postman shows that the cookie session is present in the response and the counter increments with each request. (However, I've noticed that curl includes headers and a mysession cookie)
curl --location 'localhost:8080/incr' \
--header 'Cookie: mysession=someLongKey='
Should I include this header in my fetch request? And how do I retrieve the someLongKey value to send with the request?
Currently, when attempting to make this request using fetch in a Next.js project.
// Frontend Next.js code snippet
const resp = await fetch(`${process.env.BACKEND}/incr`)
const res = await Promise.all([resp.json()])
console.log("this is the res ",res)
The output does not increment as expected,
this is the res [ { count: 0 } ]
POST /incr 200 in 16ms
this is the res [ { count: 0 } ]
POST /incr 200 in 17ms
this is the res [ { count: 0 } ]
POST /incr 200 in 15ms
this is the res [ { count: 0 } ]
Additionally, upon inspecting the browser developer tools, the cookie is not visible in the response.