我知道有很多这样的问题,但从我的尝试来看,似乎没有什么是可行的。
快速概述应用程序nodejs后端,它使用365个护照验证用户,然后在ReactJS前端使用。
我在udemy上使用课程跟踪Node,直到我开始收到以下错误,它才开始工作:
“在”"00037ffe-0944-74f2-0000-000000000000@84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa“”路径下"_id“为”_id“表示"office”的值转换为ObjectId失败
我是MongoDB和猫鼬的新手,所以我真的不知道该看什么。我想它在我的passport.use第四节的某个地方。
一旦我重新构建了MongoDB和所有与它相关的内容(基本模式等等)错误就会消失。(我指的是与新的mongo集合/实例完全相同的代码。)
然后,我得到365护照认证工作,但一段时间后,同样的错误将显示出来。如果你需要更多的片段,请告诉我,这是我到目前为止的情况:
以下是我的护照Outlook策略:
passport.use(
new OutlookStrategy(
{
clientID: keys.OUTLOOK_CLIENT_ID,
clientSecret: keys.OUTLOOK_SECRET,
callbackURL: "/authorize-outlook"
},
async (accessToken, refreshToken, profile, done) => {
const exist = await Office.findOne({ outlookID: profile.id }).catch(
error => console.log("error: ", error)
);
if (exist) {
return done(null, exist);
} else {
const user = new Office({
outlookID: profile.id,
displayName: profile.displayName
}).save();
done(null, user);
}
}
)
);
Serialize/Deserialize:
passport.serializeUser((user, done) => {
//turns user into id
// see mlabs _id, identifying ID added my mongo
// this is not profile id from google
done(null, user.id);
});
// ID into a mongoose instance
// id placed in cookie above in "serializeUser" i.e the user.id
passport.deserializeUser((id, done) => {
//turns id into user (mongoose model instance)
Office.findById(id).then(user => {
done(null, user);
});
});
My:
const mongoose = require("mongoose");
const { Schema } = mongoose;
OfficeSchema = new Schema({
outlookID: String,
displayName: String
});
mongoose.model("office", OfficeSchema);
最后是我的身份验证路由:
app.get(
"/auth/outlook",
passport.authenticate("windowslive", {
scope: [
"openid",
"profile",
"offline_access",
"https://outlook.office.com/Mail.Read"
]
})
);
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function will be called,
// which, in this example, will redirect the user to the home page.
app.get(
"/authorize-outlook",
passport.authenticate("windowslive"),
(req, res) => {
res.redirect("/dashboard");
}
);
};
我知道这不仅仅是必要的,但我宁愿现在尽可能多地付出。
如果你有修理/改进,请寄给我。
任何帮助都将不胜感激。耽误您时间,实在对不起。
发布于 2018-08-07 02:20:08
这个帮我修好了:
passport.deserializeUser((id, done) => {
Office.findById(id).then(user => {
done(null, user);
});
});
发布于 2018-07-30 01:21:41
尝试更改下面的代码
当前:
passport.deserializeUser((id, done) => {
//turns id into user (mongoose model instance)
Office.findById(id).then(user => {
done(null, user);
});
});
期望的
passport.deserializeUser((id, done) => {
//turns id into user (mongoose model instance)
Office.findById(mongoose.Types.ObjectId(id)).then(user => {
done(null, user);
});
});
我觉得这应该管用
https://stackoverflow.com/questions/51589887
复制