1

Below is sample Go code for two Google cloud functions. Each function is in a different project. I want to set a cookie in the first function (myfunction1), redirect to the second function (myfunction2), and access the cookie from that second function. I've tried a variety of parameters and values for setting the cookie but the second function always hits the error "Error getting mycookie: http: named cookie not present".

// Sample code for function https://us-central1-project1.cloudfunctions.net/myfunction1

myCookie := http.Cookie{
    userCookie := http.Cookie{
    Name: "mycookie", 
    Value: "foo", 
    Path: "/",
    MaxAge: 60 * 60,
    HttpOnly: true,
    Secure: true,
    SameSite: http.SameSiteLaxMode,
    Domain: ".cloudfunctions.net",
}
http.SetCookie(w, &myCookie)

url := "https://us-central1-project2.cloudfunctions.net/myfunction2"
http.Redirect(w, r, url, http.StatusTemporaryRedirect)

// sample code for function https://us-central1-project2.cloudfunctions.net/myfunction2

cookie, err := r.Cookie("mycookie")
if err != nil {
 log.Fatalf("Error getting mycookie: %v", err)
} 
log.Println("cookie: ", cookie.Value)
8
  • If you put Firebase Hosting in front of your functions you can effectively proxy them to the same host, and you won't have to play games with trying to get them to cross domains. Commented Jul 2, 2023 at 21:17
  • This document explains how to use and protect cookies. My recommendation is to set up Firebase Hosting and follow Doug's advice. Another option is to use a custom HTTP header to share small amounts of data. Deploying a load balancer and a domain is also possible. No matter what which method you decide on, do not deploy cross-origin cookies for domains you do not control (cloudfunctions.net). Commented Jul 2, 2023 at 22:09
  • @DougStevenson - Thanks for the suggestion. I've done nothing with Firebase so far but I'll take a look. I'm hoping someone either looks at the sample code and can point out what to change, or state it's impossible to do what I'm wanting to do without a proxy or similar additional architecure. Commented Jul 3, 2023 at 0:22
  • @JohnHanley - Thanks for the link. Good info but I don't see anything that seems to solve my issue. I do control both functions. I'd be happy to use a custom HTTP header but from what I read, HTTP headers are not sent for an HTTP redirect. Can you advise a way to set/send a custom HTTP header for an HTTP redirect? Commented Jul 3, 2023 at 0:35
  • Your function sets an HTTP header in the response to the client. The client then adds the header in its request to the other function. I am assuming that you are controlling the client code as well. If you don't then your option is to use a domain name so that you can set a same-origin cookie. That means deploying Firebase Hosting, HTTP Load Balancer, etc. Commented Jul 3, 2023 at 0:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.