gpt4 book ai didi

javascript - 通过多个 OAuth2 提供商登录时如何减少代码重复?

转载 作者:行者123 更新时间:2023-11-30 05:40:42 27 4
gpt4 key购买 nike

我正在使用 Node.jsExpressPassportMongoose Node 模块。

用户点击sign in with provider有两种情况。以下是 GitHub 策略的示例:

1。如果用户已经登录,请将此 github 帐户链接到当前登录的用户。

2。如果用户未登录,则检查它是第一次使用 Github 登录还是返回用户。

问:我可以使用 MongoDB 查询生成器或其他技术以某种方式合并此代码吗?

if (req.user) {
// Already logged in. Clicked on "Link Github account" on Settings page.
User.findById(req.user.id, function(err, user) {
// Merge Github with local account for the current user.
user.github = profile.id;
user.tokens.push({ kind: 'github', accessToken: accessToken });
user.profile.name = profile.displayName;
user.profile.email = user.profile.email || profile._json.email;
user.profile.picture = user.profile.picture || profile._json.avatar_url;
user.profile.location = user.profile.location || profile._json.location;
user.profile.website = user.profile.website || profile._json.blog;
user.save(function(err) {
done(err, user);
});
});
} else {
// Unauthenticated user arriving from Login/Signup page.
User.findOne({ github: profile.id }, function(err, existingUser) {
// Returning user. Stop here.
if (existingUser) return done(null, existingUser);
// First time. Create a new user.
var user = new User();
user.github = profile.id;
user.tokens.push({ kind: 'github', accessToken: accessToken });
user.profile.name = profile.displayName;
user.profile.email = profile._json.email;
user.profile.picture = profile._json.avatar_url;
user.profile.location = profile._json.location;
user.profile.website = profile._json.blog;
user.save(function(err) {
done(err, user);
});
});
}

最佳答案

不确定你在问什么,但在这里,你可以将类似的部分放入这样的函数中:

if (req.user) {
// Already logged in. Clicked on "Link Github account" on Settings page.
User.findById(req.user.id, function(err, user) {
// Merge Github with local account for the current user.
updateUserGithub(user, profile, done);
});
} else {
// Unauthenticated user arriving from Login/Signup page.
User.findOne({ github: profile.id }, function(err, existingUser) {
// Returning user. Stop here.
if (existingUser) return done(null, existingUser);
// First time. Create a new user.
var user = new User();
updateUserGithub(user, profile, done);
});
}

function updateUserGithub(user, profile, done) {
user.github = profile.id;
user.tokens.push({ kind: 'github', accessToken: accessToken });
user.profile.name = profile.displayName;
user.profile.email = user.profile.email || profile._json.email;
user.profile.picture = user.profile.picture || profile._json.avatar_url;
user.profile.location = user.profile.location || profile._json.location;
user.profile.website = user.profile.website || profile._json.blog;
user.save(function(err) {
done(err, user);
});
}

如果你在这里没有默认值逻辑(比如 user.profile.website || profile._json.blog;)你可以使用 mongodb update 命令而不是获取和保存整个文档。

关于javascript - 通过多个 OAuth2 提供商登录时如何减少代码重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20980585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com