在localhost上工作,我设法使一切正常。我从客户端使用google登录,这将我重定向到Google登录窗口。通常,成功登录后,我应该在home组件中,并在其中找到我的用户信息,并将cookie附加到我的请求中。
我面临的问题是,当我在本地主机上工作时,它曾经将cookie存储在客户端上,而我可以通过chrome工具看到它们。但是现在,我的正面是通过netlify上传的,而我的背面是在heroku上的,我在xxxx.herokuapp.com/上看到了所需的cookie,但在yyyy.netlify.app/中却看不到。看起来像这样:
crossing-session eyJwYXNzcG9ydCI6eyJ1c2VyIjoic2NhbWlub3JlZ3VlaXJvQGdtYWlsLmNvbSJ9fQ== xxxx.herokuapp.com / 2020-08-29T06:59:28.340Z 84 ✓ ✓ Medium
因此,我无法检索用户信息,因为我是从客户端而不是服务器发出请求的。 在一个由console.log req.user制成的中间件中(在通行证从cookie反序列化用户信息之后应该存在),我可以看到该用户仅在服务器发出请求时才存在,这就是为什么我可以看到登录的用户当我重定向到google登录弹出窗口时,系统会显示信息(请求是从服务器发出的,我猜是这样),但是当我尝试从客户端请求中检索用户信息时,却无法定义:
2020-08-28T07:31:36.512780+00:00 app[web.1]: [
2020-08-28T07:31:36.512790+00:00 app[web.1]: RowDataPacket { //Information retrieved from the cookie set before the new sign in attempt
2020-08-28T07:31:36.512791+00:00 app[web.1]: id: 1,
2020-08-28T07:31:36.512791+00:00 app[web.1]: name: 'Sergio',
2020-08-28T07:31:36.512794+00:00 app[web.1]: }
2020-08-28T07:31:36.512794+00:00 app[web.1]: ]
2020-08-28T07:31:36.638328+00:00 app[web.1]: GET /auth/google/callback?code=4%2F3gHtSiVlaf80Ura7zXJTxZox6ur3qTqy3aZsHFI8GttT3rE1csLTyWo-y1IegQ-qzTmDADvS6qPrui2DYU7wb4Y&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&authuser=0&prompt=none 302 128.336 ms - 70
2020-08-28T07:31:36.752166+00:00 app[web.1]: [
2020-08-28T07:31:36.752168+00:00 app[web.1]: RowDataPacket { //New information from the sign in stored in req.user
2020-08-28T07:31:36.752168+00:00 app[web.1]: id: 2,
2020-08-28T07:31:36.752169+00:00 app[web.1]: name: 'Jimmy',
2020-08-28T07:31:36.752172+00:00 app[web.1]: }
2020-08-28T07:31:36.752173+00:00 app[web.1]: ]
2020-08-28T07:31:36.753437+00:00 app[web.1]: GET /auth/success 302 4.078 ms - 112
2020-08-28T07:31:36.637900+00:00 heroku[router]: at=info method=GET path="/auth/google/callback?code=4%2F3gHtSiVlaf80Ura7zXJTxZox6ur3qTqy3aZsHFI8GttT3rE1csLTyWo-y1IegQ-qzTmDADvS6qPrui2DYU7wb4Y&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&authuser=0&prompt=none" host=crossingpathsserver.herokuapp.com request_id=10ace1ed-4a00-4476-b405-a63c02e26ef4 fwd="83.54.153.111" dyno=web.1 connect=1ms service=132ms status=302 bytes=511 protocol=https
2020-08-28T07:31:36.753099+00:00 heroku[router]: at=info method=GET path="/auth/success" host=crossingpathsserver.herokuapp.com request_id=f4a35e18-41dc-49eb-9b30-23ade14dec9f fwd="83.54.153.111" dyno=web.1 connect=1ms service=6ms status=302 bytes=575 protocol=https
2020-08-28T07:31:37.608293+00:00 heroku[router]: at=info method=GET path="/users/matches" host=crossingpathsserver.herokuapp.com request_id=57e2eec5-ac17-407b-97bb-0b561f74a144 fwd="83.54.153.111" dyno=web.1 connect=1ms service=3ms status=403 bytes=526 protocol=https
2020-08-28T07:31:37.576444+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=NGp-g2Q" host=crossingpathsserver.herokuapp.com request_id=aafca51e-6f9b-4f79-8e29-af2560693d5f fwd="83.54.153.111" dyno=web.1 connect=0ms service=2ms status=200 bytes=416 protocol=https
2020-08-28T07:31:37.695029+00:00 heroku[router]: at=info method=GET path="/users/current" host=crossingpathsserver.herokuapp.com request_id=3f262b88-f79d-46e3-9eea-44102082c7a0 fwd="83.54.153.111" dyno=web.1 connect=0ms service=2ms status=403 bytes=526 protocol=https
2020-08-28T07:31:37.576088+00:00 app[web.1]: A user connected
2020-08-28T07:31:37.593974+00:00 app[web.1]: undefined //Result from console.log(req.user) when access directly from the client (netlify)
2020-08-28T07:31:37.596258+00:00 app[web.1]: GET /users/current 403 3.117 ms - 9