使用 MongoDB Atlas 和 Passport.js 设置 Facebook 身份验证 [英] Setting Up Facebook Authentication with MongoDB Atlas and Passport.js
问题描述
我做了很多研究来找到我的问题的答案,但我没有发现任何东西.如果我错过了答案,请随意附上链接.
我确实找到了这个解决方案,但我不确定它如何适用于这种情况:
终于成功了!我进入了 Atlas 上的索引选项卡并删除了所需的用户名索引.感谢您的帮助@oleg 出于某种原因,我在网上找不到这个解释!
更新
所以从 Mongo-Atlas 中删除索引只能暂时解决问题.我相信我想出了如何从我的代码中删除索引,但我已经从 Mongo shell 中删除了它,这似乎也解决了问题.所以我将提供这两个命令:
const User = new mongoose.model('User', userSchema);User.collection.indexExists({ "username" : 1 }, function(err, results){控制台日志(结果);如果(结果 === 真){//删除 MongoDB 中的索引User.collection.dropIndex( { "username" : 1 } , function(err, res) {如果(错误){console.log('删除索引时出错!', err);}});} 别的 {console.log("索引不存在!");}});
在 Mongo Shell 中,进入您的数据库并运行以下命令:
db.users.dropIndex( { "username" : 1 } );
要确认,请运行:
db.users.getIndexes()
为了解决这个问题花费了大量时间进行研究!希望能帮到你!
I've done a lot of research to find this answer to my problem, but I haven't discovered anything. Feel free to attach a link if I missed the answer.
I did find this solution, but I'm not exactly sure how it might apply to this situation: E11000 duplicate key error index in mongodb mongoose
I'm trying to connect my Express.js, MongoDB, Passport.js app to Mongo-Atlas. I have my Atlas cluster set up and accepting new Users from my localhost and using Google Oauth 2.0, however, when I try to register or log-in using Facebook, I get the following error.
MongoError: E11000 duplicate key error collection: userDB.users index: username_1 dup key: { username: null }
Could it have something to do with this?
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
It worked when connected to my localhost MongoDB, and as I stated I'm having trouble finding a solution.
passport.use(new FacebookStrategy({
clientID: process.env.FBAPPID,
clientSecret: process.env.FBSECRET,
callbackURL: "http://localhost:3000/auth/facebook/secrets"
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
User.findOrCreate({ facebookId: profile.id }, function (err, user) {
return cb(err, user);
});
}
));
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/secrets',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/secrets');
});
UPDATE
So I've found some documentation about dropping Indexes on MongoDB However that wasn't entirely clear, so I also found an article Dropping Existing Indexes So I implemented the code into my own app.js file.
// Dropping an Index in MongoDB
User.collection.dropIndex({name : "username_1"}, function(err, res) {
if (err) {
console.log('Error in dropping index!', err);
}
});
I've been dropping the Database in between testing to see if that works, but I've been getting thrown this error everything, no matter how I alter the code to try and fix it!
{ _bsontype: 'Timestamp', low_: 1, high_: 1586408833 },
ok: 0,
errmsg: `can't find index with key: { name: "username_1" }`,
code: 27,
codeName: 'IndexNotFound',
'$clusterTime': {
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1586408833 },
signature: { hash: [Binary], keyId: [Long] }
},
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {}
}
And yet when I run db.users.getIndexes()
in my Mongo Shell connected to my Atlas server. Sure enough there it is! I even tried using dropIndexes
and it only dropped the email index! I am so frustrated with this!
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "userDB.users"
},
{
"v" : 2,
"key" : {
"email" : 1
},
"name" : "email_1",
"ns" : "userDB.users",
"background" : true
},
{
"v" : 2,
"unique" : true,
"key" : {
"username" : 1
},
"name" : "username_1",
"ns" : "userDB.users",
"background" : true
}
]
It finally clicked! I went into the indexes tab on Atlas and deleted the required username index. Thank you for your help @oleg For some reason I couldn't find this explanation online!
Update
So deleting the index from Mongo-Atlas only temporarily solves the problem. I believe I figured out how to drop the index from my code, but I dropped it from Mongo shell already, which has seemed to solve the problem as well. So I'll provide both commands:
const User = new mongoose.model('User', userSchema);
User.collection.indexExists({ "username" : 1 }, function(err, results){
console.log(results);
if ( results === true) {
// Dropping an Index in MongoDB
User.collection.dropIndex( { "username" : 1 } , function(err, res) {
if (err) {
console.log('Error in dropping index!', err);
}
});
} else {
console.log("Index doesn't exisit!");
}
});
In Mongo Shell, go into your DB and run this command:
db.users.dropIndex( { "username" : 1 } );
To confirm, run:
db.users.getIndexes()
So many hours of research trying to solve this problem! I hope it helps you!
这篇关于使用 MongoDB Atlas 和 Passport.js 设置 Facebook 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!