Fix duplicate email sign in
This commit is contained in:
@ -79,9 +79,31 @@ module.exports = function(User) {
|
|||||||
ctx.res.redirect('/email-signin');
|
ctx.res.redirect('/email-signin');
|
||||||
});
|
});
|
||||||
|
|
||||||
User.beforeRemote('create', function({ req }, notUsed, next) {
|
User.beforeRemote('create', function({ req, res }, _, next) {
|
||||||
req.body.username = 'fcc' + uuid.v4().slice(0, 8);
|
req.body.username = 'fcc' + uuid.v4().slice(0, 8);
|
||||||
next();
|
if (!req.body.email) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
return User.doesExist(null, req.body.email)
|
||||||
|
.then(exists => {
|
||||||
|
if (!exists) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
req.flash('error', {
|
||||||
|
msg:
|
||||||
|
`email ${req.body.email} is already in user, try signing in instead`
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.redirect('/email-signin');
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(err);
|
||||||
|
req.flash('error', {
|
||||||
|
msg: 'Oops, something went wrong, please try again later'
|
||||||
|
});
|
||||||
|
return res.redirect('/email-signup');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
User.on('resetPasswordRequest', function(info) {
|
User.on('resetPasswordRequest', function(info) {
|
||||||
@ -174,17 +196,15 @@ module.exports = function(User) {
|
|||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
User.doesExist = function doesExist(username, email, cb) {
|
User.doesExist = function doesExist(username, email) {
|
||||||
if (!username && !email) {
|
if (!username && !email) {
|
||||||
return nextTick(function() {
|
return Promise.resolve(false);
|
||||||
cb(null, false);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
debug('checking existence');
|
debug('checking existence');
|
||||||
|
|
||||||
// check to see if username is on blacklist
|
// check to see if username is on blacklist
|
||||||
if (username && blacklistedUsernames.indexOf(username) !== -1) {
|
if (username && blacklistedUsernames.indexOf(username) !== -1) {
|
||||||
return cb(null, true);
|
return Promise.resolve(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
var where = {};
|
var where = {};
|
||||||
@ -194,19 +214,8 @@ module.exports = function(User) {
|
|||||||
where.email = email ? email.toLowerCase() : email;
|
where.email = email ? email.toLowerCase() : email;
|
||||||
}
|
}
|
||||||
debug('where', where);
|
debug('where', where);
|
||||||
User.count(
|
return User.count(where)
|
||||||
where,
|
.then(count => count > 0);
|
||||||
function(err, count) {
|
|
||||||
if (err) {
|
|
||||||
debug('err checking existance: ', err);
|
|
||||||
return cb(err);
|
|
||||||
}
|
|
||||||
if (count > 0) {
|
|
||||||
return cb(null, true);
|
|
||||||
}
|
|
||||||
return cb(null, false);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
User.remoteMethod(
|
User.remoteMethod(
|
||||||
|
Reference in New Issue
Block a user