繁体   English   中英

在 Heroku 上实时部署时,passport google oauth2 会导致“内部服务器错误”

[英]passport google oauth2 causes “internal server error” when deployed live on Heroku

我通过 mongoose 连接到 mongoDB 并且在本地主机上使用我的应用程序时没有错误,但是当我将它部署到 Heroku 时,我收到了内部服务器错误。

这是我的js代码:

require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const findOrCreate = require("mongoose-findorcreate");

const app = express();

app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");

app.use(session({
  secret: varkey,
  resave: false,
  saveUninitialized: false
}));

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

mongoose.connect("mongodb+srv://admin-name:password@cluster0-7monv.mongodb.net/userDB?retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.set("useCreateIndex", true);

const userSchema = new mongoose.Schema({
  email: String,
  password: String,
  googleId: String
});

userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);

const User = new mongoose.model("User", userSchema);

passport.use(User.createStrategy());
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});

passport.use(new GoogleStrategy({
  clientID: process.env.GOOGLE_CLIENT_ID,
  clientSecret: process.env.GOOGLE_CLIENT_SECRET,
  callbackURL: "https://herokudomain.herokuapp.com/auth/google/callback",
},
  function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));

app.get("/auth/google", passport.authenticate("google", {scope : ["profile", "email"]}));

app.get("/auth/google/callback",
  passport.authenticate("google", { failureRedirect: "/login" }),
  function(req, res) {

    res.redirect("/");
});

这些是我的 html 代码:

<a class="btn" href="/auth/google" role="button">Sign Up with Google</a>

Heroku 日志在他们的错误日志中显示了这一点:

2020-06-09T04:05:10.523711+00:00 app[web.1]: MongoError: E11000 duplicate key error collection: userDB.users index: username_1 dup key: { username: null }
2020-06-09T04:05:10.523722+00:00 app[web.1]:     at Function.create (/app/node_modules/mongodb/lib/core/error.js:51:12)
2020-06-09T04:05:10.523722+00:00 app[web.1]:     at toError (/app/node_modules/mongodb/lib/utils.js:149:22)
2020-06-09T04:05:10.523723+00:00 app[web.1]:     at /app/node_modules/mongodb/lib/operations/common_functions.js:265:39
2020-06-09T04:05:10.523723+00:00 app[web.1]:     at handler (/app/node_modules/mongodb/lib/core/sdam/topology.js:913:24)
2020-06-09T04:05:10.523724+00:00 app[web.1]:     at /app/node_modules/mongodb/lib/cmap/connection_pool.js:356:13
2020-06-09T04:05:10.523724+00:00 app[web.1]:     at handleOperationResult (/app/node_modules/mongodb/lib/core/sdam/server.js:489:5)
2020-06-09T04:05:10.523725+00:00 app[web.1]:     at MessageStream.messageHandler (/app/node_modules/mongodb/lib/cmap/connection.js:270:5)
2020-06-09T04:05:10.523726+00:00 app[web.1]:     at MessageStream.emit (events.js:315:20)
2020-06-09T04:05:10.523726+00:00 app[web.1]:     at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
2020-06-09T04:05:10.523726+00:00 app[web.1]:     at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
2020-06-09T04:05:10.523727+00:00 app[web.1]:     at writeOrBuffer (_stream_writable.js:352:12)
2020-06-09T04:05:10.523727+00:00 app[web.1]:     at MessageStream.Writable.write (_stream_writable.js:303:10)
2020-06-09T04:05:10.523728+00:00 app[web.1]:     at TLSSocket.ondata (_stream_readable.js:712:22)
2020-06-09T04:05:10.523728+00:00 app[web.1]:     at TLSSocket.emit (events.js:315:20)
2020-06-09T04:05:10.523729+00:00 app[web.1]:     at addChunk (_stream_readable.js:302:12)
2020-06-09T04:05:10.523729+00:00 app[web.1]:     at readableAddChunk (_stream_readable.js:278:9)

而 web 应用程序返回“内部服务器错误”页面。 似乎他们在说我的数据库有重复,但我已经清除了我的数据库并且那里没有任何内容了。

请帮助我,我对编程很陌生,所以我仍在学习如何阅读错误代码并理解它们,以便我自己调试。

我已经看到了类似的问题并尝试了提供的解决方案,但它们都不适用于我的情况,这就是我决定提出这个问题的原因。

为什么你有错误:

从错误日志中:

2020-06-09T04:05:10.523711+00:00 app[web.1]: MongoError: E11000 duplicate key error collection: userDB.users index: username_1 dup key: { username: null }
...

我可以推断您在users集合中的username名字段上有一个唯一索引,并且因为您有一个username名字段为空(null)的现有文档,MongoDB 阻止您插入另一个username值为空的文档。

从您发布的代码中,我可以看到 User 架构不包含username段,也许您以前曾经在架构中拥有它。 您应该注意,根据您清除数据库中数据的方式,索引可能会或可能不会被清除,在这种情况下, username名字段上的索引没有被清除。

修复:

删除username属性上的索引。 在 mongo CLI 中运行db.users.dropIndex("username_1")来做到这一点。 您可以在之后运行db.users.getIndexes()以确保索引只存在于您需要的地方。

由于根据 MongoDB 它认为用户名是唯一的,因此不会让 null 值第二次进入数据库。 当您从谷歌获取个人资料时,您需要发送用户名。 我的代码如下

    (accessToken, refreshToken, profile, cb) => {
        console.log(profile);
        //install and require find or create to make following function work
        User.findOrCreate({
            googleId: profile.id,
            username: profile.emails[0].value
        }, (err, user) => {
            return cb(err, user);
        });
    }
));

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM