lundi 19 juillet 2021

Cookie is not being set in the frontend even if it is present in the network tab

I'm using golang and mux for the backend and simple html for the frontend. The code for setting a cookie in the response (not full):

import  "github.com/gorilla/sessions" // this is where sessions comes from

var store = sessions.NewCookieStore([]byte("secret"))
store.Options = &sessions.Options{
        MaxAge:   3600 * 24,
        HttpOnly: true,
        Path: "/",
        Secure: true,
    }
session, _ := store.Get(request, "uid")
session.Values["uid"] = token
err = session.Save(request, writer)
if err != nil {
    log.Fatalln(err)
    return
}

and this is how I fetch:

fetch("http://localhost:8000/user/register", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          credentials: 'include',
          body: JSON.stringify(user),
        })

Also I have cors enabled on the backend:

c := cors.New(cors.Options{
        AllowedOrigins: []string{"http://127.0.0.1:3000"},
        AllowCredentials: true,
    })

The content of the set-cookie header:

Set-Cookie: uid=jwt_token; Path=/; Expires=Tue, 20 Jul 2021 08:42:37 GMT; Max-Age=86400; HttpOnly; Secure

The cookie is not being set, but in the network tab the 'set-cookie' header is present. If you need more details about my code, ask in the comments and I will post a link to a pastebin.




Aucun commentaire:

Enregistrer un commentaire