LibreChat/api/strategies/googleStrategy.js
Danny Avila 12209fe0dd
refactor: address potential issues with deploy-compose.yml (#1220)
* chore: remove /config/loader

* chore: remove config/loader steps from Dockerfile.multi

* chore: remove install script
2023-11-25 16:34:51 -05:00

46 lines
1.3 KiB
JavaScript

const { Strategy: GoogleStrategy } = require('passport-google-oauth20');
const User = require('../models/User');
const googleLogin = async (accessToken, refreshToken, profile, cb) => {
try {
const email = profile.emails[0].value;
const googleId = 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: 'google',
googleId,
username: profile.name.givenName,
email,
emailVerified: profile.emails[0].verified,
name: `${profile.name.givenName} ${profile.name.familyName}`,
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);
}
};
module.exports = () =>
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: `${process.env.DOMAIN_SERVER}${process.env.GOOGLE_CALLBACK_URL}`,
proxy: true,
},
googleLogin,
);