* fixes #760

* fixes linting errors
This commit is contained in:
Cassie Tarakajian 2019-05-01 15:38:11 -04:00 committed by GitHub
parent 9d22c3c146
commit 870d9ceded
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,5 @@
import slugify from 'slugify';
import friendlyWords from 'friendly-words';
import User from '../models/user'; import User from '../models/user';
const lodash = require('lodash'); const lodash = require('lodash');
@ -110,7 +112,7 @@ passport.use(new GoogleStrategy({
clientSecret: process.env.GOOGLE_SECRET, clientSecret: process.env.GOOGLE_SECRET,
callbackURL: '/auth/google/callback', callbackURL: '/auth/google/callback',
passReqToCallback: true, passReqToCallback: true,
scope: ['email'], scope: ['openid email'],
}, (req, accessToken, refreshToken, profile, done) => { }, (req, accessToken, refreshToken, profile, done) => {
User.findOne({ google: profile._json.emails[0].value }, (findByGoogleErr, existingUser) => { User.findOne({ google: profile._json.emails[0].value }, (findByGoogleErr, existingUser) => {
if (existingUser) { if (existingUser) {
@ -123,24 +125,43 @@ passport.use(new GoogleStrategy({
User.findOne({ User.findOne({
email: primaryEmail, email: primaryEmail,
}, (findByEmailErr, existingEmailUser) => { }, (findByEmailErr, existingEmailUser) => {
if (existingEmailUser) { let username = profile._json.emails[0].value.split('@')[0];
existingEmailUser.email = existingEmailUser.email || primaryEmail; User.findOne({ username }, (findByUsernameErr, existingUsernameUser) => {
existingEmailUser.google = profile._json.emails[0].value; if (existingUsernameUser) {
existingEmailUser.username = existingEmailUser.username || profile._json.emails[0].value; const adj = friendlyWords.predicates[Math.floor(Math.random() * friendlyWords.predicates.length)];
existingEmailUser.tokens.push({ kind: 'google', accessToken }); username = slugify(`${username} ${adj}`);
existingEmailUser.name = existingEmailUser.name || profile._json.displayName; }
existingEmailUser.verified = User.EmailConfirmation.Verified; // what if a username is already taken from the display name too?
existingEmailUser.save(saveErr => done(null, existingEmailUser)); // then, append a random friendly word?
} else { if (existingEmailUser) {
const user = new User(); existingEmailUser.email = existingEmailUser.email || primaryEmail;
user.email = primaryEmail; existingEmailUser.google = profile._json.emails[0].value;
user.google = profile._json.emails[0].value; existingEmailUser.username = existingEmailUser.username || username;
user.username = profile._json.emails[0].value; existingEmailUser.tokens.push({ kind: 'google', accessToken });
user.tokens.push({ kind: 'google', accessToken }); existingEmailUser.name = existingEmailUser.name || profile._json.displayName;
user.name = profile._json.displayName; existingEmailUser.verified = User.EmailConfirmation.Verified;
user.verified = User.EmailConfirmation.Verified; existingEmailUser.save((saveErr) => {
user.save(saveErr => done(null, user)); if (saveErr) {
} console.log(saveErr);
}
done(null, existingEmailUser);
});
} else {
const user = new User();
user.email = primaryEmail;
user.google = profile._json.emails[0].value;
user.username = username;
user.tokens.push({ kind: 'google', accessToken });
user.name = profile._json.displayName;
user.verified = User.EmailConfirmation.Verified;
user.save((saveErr) => {
if (saveErr) {
console.log(saveErr);
}
done(null, user);
});
}
});
}); });
}); });
})); }));