部署到 Heroku 后,我无法登录应用程序。我成功构建和部署了,但是当我启动应用程序时,它开始注销,并在尝试登录时挂起并引发应用程序错误。 Express/Passport/MongoDB + create-react-app
我检查了我的 key 好几次,珠穆朗玛峰匹配完美。在我的本地服务器上everesing工作完美。可能是服务器上的“https”响应有问题吗?
应用程序链接 https://mysterious-thicket-20426.herokuapp.com/
git 仓库 https://github.com/detoner777/email-feedback-app
heroku 错误日志:
2019-08-20T09:44:08.211815+00:00 heroku[router]: at=错误代码=H12 desc="请求超时"method=GET path="/auth/google/callback?code=4% 2FpAFASjHeS_JuqGKXbEIDY4Jh5zHBJZk-Zyg19q6DHWGVtipmCKVt1hLpwKltJUHW9XB3EBLcPGorEQUI68uZCE&scope=电子邮件+个人资料+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapi s.com%2Fauth%2Fuserinfo.profile&authuser=0&session_state=330af188f3789437fd21ab099ebe776784cfe500..3fb5&提示=无“主机=神秘的丛林-20426.herokuapp.com request_id = 59b62556-d07c-42ae-8f7f-e89b3ab5b735 fwd =“31.134.127.229”dyno = web.1连接= 1ms服务= 30000ms状态= 503字节= 0协议(protocol)=https 2019-08-20T09:52:03.838240 + 00:00 heroku [路由器]:at = info method = HEAD path =“/”主机= mysterious-thicket-20426.herokuapp.com request_id = 81a31581-1aa6-478d-98af- 68b6c479e32a fwd="217.182.175.162"dyno=web.1 连接=0ms 服务=1ms 状态=200 字节=289 协议(protocol)=https
//inex.js
const express = require('express');
const mongoose = require('mongoose');
const cookieSession = require('cookie-session');
const passport = require('passport');
const bodyParser = require('body-parser');
const keys = require('./config/keys');
require('./models/User');
require('./services/passport');
mongoose.connect(keys.mongoURI);
const app = express();
app.use(bodyParser.json());
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: [keys.cookieKey]
})
);
app.use(passport.initialize());
app.use(passport.session());
require('./routes/authRoutes')(app);
require('./routes/billingRoutes')(app);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT);
// passport.js
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');
const User = mongoose.model('users');
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id).then(user => {
done(null, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback',
proxy: true
},
async (accessToken, refreshToken, profile, done) => {
const existingUser = await User.findOne({ googleId: profile.id });
if (existingUser) {
return done(null, existingUser);
}
const user = await User({ googleID: profile.id }).save()
done(null, user);
}
)
);
最佳答案
解决了,这对我有用:
请检查您的 Mongodb Atlas prod 数据库的用户/密码是否正确匹配您在 Heroku 中设置的 MONGO_URI 变量,这对我来说是导致此问题的原因。我认为这里很容易出现拼写错误,因为创建数据库用户后您看不到密码。另请注意,用户不应是“只读”的,并且这与您用于登录 Mongodb Atlas 的用户/密码组合不同。
我发现身份验证回调因超时错误而失败的原因是文件passport.js中定义的Google策略回调从未调用方法done()。反过来,发生这种情况是因为在 MongoDB 中查找用户的查询从未完成,因为 Mongoose 无法连接到 mlab.com 数据库。我尝试在 mlab 中为 prod 数据库创建一个新用户,并在 Heroku 环境变量中更新它,它成功了!
关于node.js - 部署到 Heroku 时无法使用 Google OAuth 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57571002/