forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Disable Registration with social login (danny-avila#813)
* Google, Github and Discord * update .env.example with ALLOW_SOCIAL_REGISTRATION * fix some conflict * refactor strategy * Update user_auth_system.md * Update user_auth_system.md
- Loading branch information
Showing
8 changed files
with
249 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,49 @@ | ||
const { Strategy: GitHubStrategy } = require('passport-github2'); | ||
const User = require('../models/User'); | ||
const config = require('../../config/loader'); | ||
const domains = config.domains; | ||
|
||
const User = require('../models/User'); | ||
const githubLogin = async (accessToken, refreshToken, profile, cb) => { | ||
try { | ||
const email = profile.emails[0].value; | ||
const githubId = profile.id; | ||
const oldUser = await User.findOne({ email }); | ||
const ALLOW_SOCIAL_REGISTRATION = | ||
process.env.ALLOW_SOCIAL_REGISTRATION?.toLowerCase() === 'true'; | ||
|
||
if (oldUser) { | ||
oldUser.avatar = profile.photos[0].value; | ||
await oldUser.save(); | ||
return cb(null, oldUser); | ||
} else if (ALLOW_SOCIAL_REGISTRATION) { | ||
const newUser = await new User({ | ||
provider: 'github', | ||
githubId, | ||
username: profile.username, | ||
email, | ||
emailVerified: profile.emails[0].verified, | ||
name: profile.displayName, | ||
avatar: profile.photos[0].value, | ||
}).save(); | ||
|
||
return cb(null, newUser); | ||
} | ||
|
||
return cb(null, false, { message: 'User not found.' }); | ||
} catch (err) { | ||
console.error(err); | ||
return cb(err); | ||
} | ||
}; | ||
|
||
// GitHub strategy | ||
const githubLogin = async () => | ||
module.exports = () => | ||
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) { | ||
oldUser.avatar = profile.photos[0].value; | ||
await oldUser.save(); | ||
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); | ||
} | ||
scope: ['user:email'], | ||
}, | ||
githubLogin, | ||
); | ||
|
||
module.exports = githubLogin; |
Oops, something went wrong.