简体   繁体   English

在 localhost 中登录,但在 Heroku 中部署时出现“会话所需的秘密选项”错误

[英]Login working in localhost but error “secret option required for sessions” when deployed in Heroku

My authentification works properly on localhost but gives me error 500 when deployed on Heroku.我的身份验证在 localhost 上正常工作,但在 Heroku 上部署时出现错误 500。

Error:错误:

{"type":"error","error":{"message":"secret option required for sessions"}}

I have my secret session on a.env file that is ignored by.gitignore when pushing (maybe I should change that?)我在 a.env 文件上有我的秘密 session,在推送时被 .gitignore 忽略(也许我应该改变它?)

Heroku Logs: Heroku 日志:

2019-10-23T17:22:22.682593+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=apppack-demo.herokuapp.com request_id=bb235945-cb82-4168-91ce-fd19d2109801 fwd="85.240.87.39" dyno=web.1 connect=0ms service=2ms status=304 bytes=237 protocol=https
2019-10-23T17:22:22.793594+00:00 heroku[router]: at=info method=GET path="/logo192.png" host=apppack-demo.herokuapp.com request_id=8a1d2243-45c9-4919-b2ad-3ee8f9148d9c fwd="85.240.87.39" dyno=web.1 connect=0ms service=2ms status=304 bytes=238 protocol=https
2019-10-23T17:22:35.594349+00:00 heroku[router]: at=info method=POST path="/api/signup" host=apppack-demo.herokuapp.com request_id=43907374-4de3-4658-92ce-9188f03e1624 fwd="85.240.87.39" dyno=web.1 connect=0ms service=3ms status=500 bytes=300 protocol=https
2019-10-23T17:22:35.592607+00:00 app[web.1]: POST /api/signup 500 1.206 ms - 74

I was facing the same issue while I was deploying my code into AWS Elastic BeanStalk.我在将代码部署到 AWS Elastic BeanStalk 时遇到了同样的问题。 In.env we could have -在.env 我们可以 -

secret='my_secret'

While in server.js:在 server.js 中:

app.use(cookieParser())
app.use(session({
  resave:true,
  saveUninitialized:true,
  secret:process.env.secret,
  cookie:{maxAge:3600000*24}
}))

The solution turned out to be adding secret to the environments in there.结果证明,解决方案是为那里的环境添加秘密。 Just add it to your enviroments for production, be it heroku or AWS.只需将其添加到您的生产环境中,无论是 heroku 还是 AWS。

Have you added your secret session from your.env file to heroku config?您是否已将您的秘密 session 从 your.env 文件添加到 heroku 配置? Since your.env is in your.gitignore it will not be pushed up to heroku.由于 your.env 在 your.gitignore 中,它不会被推送到 heroku。 Your code is looking for a string from your process.env but the heroku environment does not have it yet.您的代码正在从您的 process.env 中寻找一个字符串,但 heroku 环境还没有它。

You have two solutions你有两个解决方案

  1. Go to your app console and click on settings. Go 到您的应用程序控制台并单击设置。 Once you are there, click on the box that says reveal config vars and add your secret there.一旦你在那里,点击显示配置变量的框并在那里添加你的秘密。

or或者

  1. In the root directory of your project you can set your config vars there using the command在项目的根目录中,您可以使用以下命令在此处设置配置变量
heroku config:set SECRET_SESSION="secretName"

I recently had this issue with my app.我最近在我的应用程序中遇到了这个问题。 I was getting the 'Error: secret option required for sessions' ONLY when deployed to Heroku.仅当部署到 Heroku 时,我才收到“错误:会话所需的秘密选项”。

Here is what my code looked like originally:这是我的代码最初的样子:

app.use(session({
  secret: process.env.SESSION_SECRET, 
  resave: false, 
  saveUninitialized: false
}))

When I deployed to Heroku it kept giving me an "Internal server error".当我部署到 Heroku 时,它一直给我一个“内部服务器错误”。 Once checking the logs, it showed me 'Error: secret option required for sessions'.检查日志后,它向我显示“错误:会话所需的秘密选项”。

Here is how I fixed it:这是我修复它的方法:

app.use(session({
  secret: 'secretidhere', 
  resave: false, 
  saveUninitialized: false
}))

Since my.env file wasn't viewable and that's where I had my secret code, it was giving me that error.由于 my.env 文件不可见,而这正是我拥有密码的地方,所以它给了我这个错误。 Now, just by putting an arbitrary string 'secretidhere', I deployed to heroku again and it worked!现在,只需将任意字符串“secretidhere”,我再次部署到 heroku 并且它起作用了!

** However, please note that this should not be a permanent solution. ** 但是,请注意,这不应该是永久的解决方案。 As the poster above states, you should have a config file in your root directory, or another method so this session stays secret.正如上面的海报所说,你应该在你的根目录中有一个配置文件,或者其他方法,这样这个 session 就会保密。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的节点应用程序在 localhost 上运行良好,但是当我部署在 heroku 上时,它给了我应用程序错误。 你可以阅读下面的描述 - my node app is working fine on localhost but when i deployed on heroku , it is giving me application error. you can read description below 部署到 Heroku 时无法使用 Google OAuth 登录 - Unable to login with Google OAuth when deployed to Heroku 图像上传功能在部署的 Heroku 应用程序上不起作用,但在 Localhost 上起作用? - Image upload functionality not working on deployed Heroku app but working on Localhost? 找不到模块:在Heroku上部署时出错 - Module not found: error when deployed on Heroku 部署到Heroku时出现内部服务器错误 - Internal server error when deployed to Heroku 该应用程序在本地主机上运行良好,但 Heroku 在尝试使用 post 登录或注册时出现错误的请求错误 - The app works fine with localhost but Heroku gives bad request error when trying to login or signup with post NodeJS - 需要错误密码 - NodeJS - Error secret required 部署到 heroku 时 express-rate-limit 不起作用 - express-rate-limit not working when deployed to heroku 部署到heroku时,NodeJS视频聊天应用程序无法正常运行 - NodeJS video chat app not working properly when deployed to heroku 注册/登录只能在Localhost上使用,而不能在Heroku上使用 - Signup/login works on Localhost but not on Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM