【问题标题】:Sending Cookie from Back in Go it's an api rest, to Front-end with React JS在 Go 中从 Back 发送 Cookie,这是一个 api 休息,使用 React JS 发送到前端
【发布时间】:2020-08-23 22:05:05
【问题描述】:

大家好,我怀疑我正在学习 golang 以及如何构建和使用 API。我几乎在服务器端 (Golang) 上完成了所有工作,在前端 (React.Js) 中,我可以使用 Axios 发出获取/发布请求以进行登录、注册、发布(该应用程序将成为一个论坛)。问题是当我想保护一些路由时,当我试图从服务器端设置 Cookie 时,前端会得到一个空的 Cookie,但相反,我可以在前端设置一个 cookie。我应该在哪里创建和管理 cookie 和会话?

我会放一些代码。

Golang:

处理程序的标头:

w.Header().Set("Content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "true")

曲奇饼:

func SetCookie (w http.ResponseWriter) {
cookie := http.Cookie{
    Name: "LoginCookie",
    Value: "LoginCookieValue",
    Path: "/",
}
http.SetCookie(w, &cookie)

}

服务器通过前端AJAX Request(Axios)在浏览器中的Cookie响应:

Cookie: {Name: "", Value: "", Path: "", Domain: "", Expires: "0001-01-01T00:00:00Z", …}

但是使用 React 我可以创建一个 cookie 并在浏览器中查看:

      setCookie() {
    const cookieName = 'LoginCookie'
    const cookieVal = uuidv4()
    const cookies = new Cookies();
    cookies.set(cookieName, cookieVal , { path: '/' });
    console.log(cookies.get(cookieName));
  }

我该怎么办?还是谢谢。

编辑:在这里,我让完整的 Axios 请求作为成员建议:

   async submit (event) {

var userLogin = {
  email: this.state.email,
  password: this.state.password
}

let axiosConfig = {
  headers: {
    'Content-Type': 'application/json'
  }
}

   event.preventDefault();

   await axios.post(`http://localhost:2000/users/sign-in/`, userLogin, axiosConfig,
     {withCredentials: true})
   .then(response => {
       console.log(response)
       console.log(response.data)
       console.log(response.data.status)
       console.log(response.data.cookie)
       if (response.data.status === 200) {
         this.setCookie()
       }
       this.clearFieldLogin()
       
   }).catch(err => console.log(err))

  }

  clearFieldLogin(){
    document.getElementById("formLogin").reset()
  }

【问题讨论】:

  • 不,彼得,我在发布我的问题之前看到了那个帖子。
  • 你应该为fetch使用credentials: 'include'选项
  • 我正在使用 Axios,我应该使用 fetch 吗?我必须在哪里放置凭据:'include'
  • 哦,不好意思,我以为你在使用 fetch。

标签: reactjs http go cookies axios


【解决方案1】:

你需要在后端设置cookie并在react中读取,{ withCredentials: true }选项应该在axiosConfig对象中:

axiosConfig["withCredentials"] = true
await axios.post(
    `http://localhost:2000/users/sign-in/`,
     userLogin, 
     axiosConfig,
 )

你的错误代码:

await axios.post(
   `http://localhost:2000/users/sign-in/`,
    userLogin, 
    axiosConfig,
    {withCredentials: true}
)

【讨论】:

    【解决方案2】:

    我应该建议检查您的 AJAX 请求代码。 我认为您在向服务器端发送请求的配置有误。

    你有withCredentials: true 选项吗?

    其次,您可以尝试将 Expires 设置为您的服务器端 Cookie 对象。

    expiration := time.Now().Add(365 * 24 * time.Hour)
    cookie := http.Cookie{Name: "username", Value: "astaxie", Expires: expiration}
    http.SetCookie(w, &cookie)
    

    【讨论】:

    • 现在有完整的AJAX请求,你可以查看一下。关于到期时间我将被设置,但首先我需要让它工作。我设置了 withCredential: true。谢谢。
    猜你喜欢
    • 2021-01-29
    • 2015-05-11
    • 1970-01-01
    • 2017-05-02
    • 2021-08-28
    • 2015-04-06
    • 2017-09-02
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多