Home > other >  Cross Domain Cookie Golang ReactJs
Cross Domain Cookie Golang ReactJs

Time:04-20

In Go, I am setting the cookie for frontend:

http.SetCookie(w, &http.Cookie{
            Name:     "jwt-token",
            Value:    tokenString,
            Expires:  expirationTime,
        })

Also, I am setting these response headers in Go:

w.Header().Set("Access-Control-Allow-Origin", "https://domainB.com")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,access-control-allow-origin, access-control-allow-headers,access-control-allow-credentials")
w.Header().Set("Content-Type", "application/json")

This backend is deployed on https://domainA.com, and the frontend is deployed on https://domainB.com. The frontend is receiving the cookie from this backend in the response header, but it is not sending the cookie to backend in request header.

How to solve this issue?

CodePudding user response:

For your case, you need to add Path=/; into Set-Cookie in response headers. So that the cookie from response could be added to sequenced requests after successful login.

CodePudding user response:

Solved by updating setting the cookie to this:

http.SetCookie(w, &http.Cookie{
        Name:    "jwt-token",
        Value:   tokenString,
        Expires: expirationTime,
        SameSite: http.SameSiteNoneMode,
        Secure: true,
    })
  • Related