-1

I am writing a simple Login handler. Everything works just fine except setting the cookie.

Here is the code :

.
.
.
cookie := &http.Cookie{
        Name:     credentials.EmailAddress,
        Value:    sessionKey,
        MaxAge:   600,
        Path:     "/",
        Domain:   "localhost",
        Secure:   false,
        HttpOnly: false,
        }

http.SetCookie(ctx.Writer, cookie)

SendResponse(ctx, Response{
    Status:  http.StatusOK,
    Message: "successfully logged in",
})
return

I also tried with using Gin Gonic method, but same thing :

.
.
.
ctx.SetCookie("name", "value", 600, "/", "localhost", false, false)

SendResponse(ctx, Response{
    Status:  http.StatusOK,
    Message: "successfully logged in",
})
return

I am not sure what am I doing wrong. I don't get the cookie nor in Postman nor in browser. If anybody knows the solution, thanks in advance.

David
  • 47
  • 3
  • Don’t try domain cookies on localhost. Make sure name and value are actually valid (e.g. a email address is not a valid cookie name). How does the response look like if inspected with curl -v? – Volker Jul 31 '23 at 15:46
  • It is possible that SendResponse changes cookies after you set them. – mrasoolmirza Jul 31 '23 at 15:50
  • @Volker Same happens weather domain is localhost or not. Values are valid. Response got with curl is nothing different than the one I get with Postman or Browser : Content-Type: application/json; charset=utf-8 Date: Tue, 01 Aug 2023 15:56:48 GMT Content-Length: 15 – David Aug 01 '23 at 15:59
  • @mrasoolmirza Same happens weather I have that part of the code or not. – David Aug 01 '23 at 16:00
  • @David What is ctx? If you provide more of your code, like your entire handler function, maybe we can help. – mrasoolmirza Aug 01 '23 at 17:23

1 Answers1

0

It turns out that something wasn't valid after all. I changed cookie name to "session-cookie" and value to session uuid and now it works. I am not sure what was the problem with previous code since I did not use any non ASCII characters. If anyone knows the rules for setting the name and value of a cookie, feel free to share. Anyways, thanks to @Volker and @mrasoolmirza for the help.

David
  • 47
  • 3