'Cross Domain Cookie Golang ReactJs

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?



Solution 1:[1]

Solved by updating setting the cookie to this (used SameSite):

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dave Thommy Roxie