【发布时间】:2019-12-10 02:12:06
【问题描述】:
我正在为 ReactJS 前端构建一个 nodeJS 后端,并且想知道处理身份验证 某些 用户的最佳方式。
到目前为止,我基本上是如果用户在我的 mongoDB 集合中,身份验证流程会按预期工作。
MongoDB 架构
OfficeSchema = new Schema({
outlookID: String,
displayName: String
});
我正在使用 windows-live 身份验证(使用 Outlook 进行身份验证)。当前的实现如下所示。
passport.use(
new OutlookStrategy(
{
clientID: keys.OUTLOOK_CLIENT_ID,
clientSecret: keys.OUTLOOK_SECRET,
callbackURL: "/authorize-outlook"
},
async (accessToken, refreshToken, profile, done) => {
const existingUser = await Office.findOne({ outlookID: profile.id });
if (existingUser) {
console.log("existing: ", existingUser);
return done(null, existingUser);
} else {
console.log("no user found!");
return done(null, false);
}
}
)
);
最后,这是我的路线:
app.get(
"/auth/outlook",
passport.authenticate("windowslive", {
scope: [
"openid",
"profile",
"offline_access",
"https://outlook.office.com/Mail.Read"
]
})
);
app.get(
"/authorize-outlook",
passport.authenticate("windowslive", { failureRedirect: "/login_failure" }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect("/");
}
);
这是我的问题:
- 目前我不知道在我的策略中使用 done() 函数的最佳方法,任何提示都会很棒。
- 如何在我的身份验证流程中为未经授权的用户(不在数据库中)传递错误消息,静态消息很好(“你没有被授权”)
- 将用户添加到此数据库的最佳方式是什么(因为我目前为用户存储他们的 OutlookID)
回答我的任何问题都会有很大帮助。
感谢您的宝贵时间。
【问题讨论】:
标签: javascript node.js passport.js