mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
refactor: improve passport strategy handling in async/await manner to prevent race conditions upon importing modules (#682)
This commit is contained in:
parent
e38483a8b9
commit
6943f1c2c7
12 changed files with 273 additions and 246 deletions
|
|
@ -1,4 +1,3 @@
|
|||
const passport = require('passport');
|
||||
const { Strategy: GitHubStrategy } = require('passport-github2');
|
||||
const config = require('../../config/loader');
|
||||
const domains = config.domains;
|
||||
|
|
@ -6,42 +5,43 @@ const domains = config.domains;
|
|||
const User = require('../models/User');
|
||||
|
||||
// GitHub strategy
|
||||
const githubLogin = new GitHubStrategy(
|
||||
{
|
||||
clientID: process.env.GITHUB_CLIENT_ID,
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
||||
callbackURL: `${domains.server}${process.env.GITHUB_CALLBACK_URL}`,
|
||||
proxy: false,
|
||||
scope: ['user:email'], // Request email scope
|
||||
},
|
||||
async (accessToken, refreshToken, profile, cb) => {
|
||||
try {
|
||||
let email;
|
||||
if (profile.emails && profile.emails.length > 0) {
|
||||
email = profile.emails[0].value;
|
||||
const githubLogin = async () =>
|
||||
new GitHubStrategy(
|
||||
{
|
||||
clientID: process.env.GITHUB_CLIENT_ID,
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
||||
callbackURL: `${domains.server}${process.env.GITHUB_CALLBACK_URL}`,
|
||||
proxy: false,
|
||||
scope: ['user:email'], // Request email scope
|
||||
},
|
||||
async (accessToken, refreshToken, profile, cb) => {
|
||||
try {
|
||||
let email;
|
||||
if (profile.emails && profile.emails.length > 0) {
|
||||
email = profile.emails[0].value;
|
||||
}
|
||||
|
||||
const oldUser = await User.findOne({ email });
|
||||
if (oldUser) {
|
||||
return cb(null, oldUser);
|
||||
}
|
||||
|
||||
const newUser = await new User({
|
||||
provider: 'github',
|
||||
githubId: profile.id,
|
||||
username: profile.username,
|
||||
email,
|
||||
emailVerified: profile.emails[0].verified,
|
||||
name: profile.displayName,
|
||||
avatar: profile.photos[0].value,
|
||||
}).save();
|
||||
|
||||
cb(null, newUser);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
cb(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const oldUser = await User.findOne({ email });
|
||||
if (oldUser) {
|
||||
return cb(null, oldUser);
|
||||
}
|
||||
|
||||
const newUser = await new User({
|
||||
provider: 'github',
|
||||
githubId: profile.id,
|
||||
username: profile.username,
|
||||
email,
|
||||
emailVerified: profile.emails[0].verified,
|
||||
name: profile.displayName,
|
||||
avatar: profile.photos[0].value,
|
||||
}).save();
|
||||
|
||||
cb(null, newUser);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
cb(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
passport.use(githubLogin);
|
||||
module.exports = githubLogin;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue