部署到HTTPS后无法访问req.user

时间:2019-06-16 04:47:37

标签: javascript node.js authentication passport.js

我正在构建一个Web应用程序,该应用程序要求其用户登录,并且使用passport.js req.user对象构建功能。在本地主机上工作时,这不会引起任何问题。但是,在部署之后,应用程序报告req.user是“未定义的”。

我已经检查了password.js代码,它类似于过去部署时已经起作用的项目。我见过另一个有类似问题的stackoverflow帖子,但解决方案是确保此代码在server.js文件中。

app.use(
  session({
   store: new redis({ url: process.env.REDIS_URL }),
    secret: process.env.REDIS_SECRET,
    resave: false,
    saveUninitialized: false,
  }),
);

app.use(passport.initialize());
app.use(passport.session());

我希望我可以像在localhost中一样获取req.user对象,但是在生产中,我将收到以下错误。

ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":500,"statusText":"OK","url":"https://mylingual.me/api/decks","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://mylingual.me/api/decks: 500 OK","error":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>TypeError: Cannot read property &#39;id&#39; of undefined<br> &nbsp; &nbsp;at router.route.get (/app/server/routes/decks.js:21:32)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at next (/app/node_modules/express/lib/router/route.js:137:13)<br> &nbsp; &nbsp;at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at /app/node_modules/express/lib/router/index.js:281:22<br> &nbsp; &nbsp;at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)<br> &nbsp; &nbsp;at next (/app/node_modules/express/lib/router/index.js:275:10)<br> &nbsp; &nbsp;at Function.handle (/app/node_modules/express/lib/router/index.js:174:3)<br> &nbsp; &nbsp;at router (/app/node_modules/express/lib/router/index.js:47:12)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)<br> &nbsp; &nbsp;at /app/node_modules/express/lib/router/index.js:284:7<br> &nbsp; &nbsp;at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)<br> &nbsp; &nbsp;at next (/app/node_modules/express/lib/router/index.js:275:10)<br> &nbsp; &nbsp;at SessionStrategy.strategy.pass (/app/node_modules/passport/lib/middleware/authenticate.js:338:9)</pre>\n</body>\n</html>\n"}
    at resolvePromise (zone.js:831)
    at resolvePromise (zone.js:788)
    at zone.js:892
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:17290)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
    at drainMicroTaskQueue (zone.js:601)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:502)
    at invokeTask (zone.js:1744)

编辑#1:不确定是否会出现问题,但是我的应用程序也在HTTPS中运行。

0 个答案:

没有答案