fix(User): Unify old remove save hook with before save

Since we are no longer using remote api to create accounts those hooks
are useless. Unifying them will ensure they apply to all
save/update/creates
This commit is contained in:
Berkeley Martinez
2017-12-28 20:37:10 -08:00
committed by mrugesh mohapatra
parent d5e7bd586d
commit e60ff3775b

View File

@ -138,59 +138,63 @@ module.exports = function(User) {
); );
}); });
User.beforeRemote('create', function({ req }) { User.observe('before save', function(ctx) {
const body = req.body; const beforeCreate = Observable.of(ctx)
.filter(({ isNewInstance }) => isNewInstance)
// User.create
.map(({ instance }) => instance)
.flatMap(user => {
// note(berks): we now require all new users to supply an email // note(berks): we now require all new users to supply an email
// this was not always the case // this was not always the case
if ( if (
typeof body.email !== 'string' || typeof user.email !== 'string' ||
!isEmail(body.email) !isEmail(user.email)
) { ) {
return Promise.reject(createEmailError()); throw createEmailError();
} }
// assign random username to new users // assign random username to new users
// actual usernames will come from github // actual usernames will come from github
body.username = 'fcc' + uuid.v4(); // use full uuid to ensure uniqueness
if (body) { user.username = 'fcc' + uuid.v4();
// this is workaround for preventing a server crash
// we do this on create and on save if (!user.progressTimestamps) {
// refer strongloop/loopback/#1364 user.progressTimestamps = [];
if (body.password === '') {
body.password = null;
} }
// set email verified false on user email signup
// should not be set with oauth signin methods if (user.progressTimestamps.length === 0) {
body.emailVerified = false; user.progressTimestamps.push({ timestamp: Date.now() });
} }
return User.doesExist(null, body.email) return Observable.fromPromise(User.doesExist(null, user.email))
.catch(err => { .do(exists => {
throw wrapHandledError(err, { redirectTo: '/email-signup' }); if (exists) {
}) throw wrapHandledError(
.then(exists => {
if (!exists) {
return null;
}
const err = wrapHandledError(
new Error('user already exists'), new Error('user already exists'),
{ {
redirectTo: '/email-signin', redirectTo: '/email-signin',
message: dedent` message: dedent`
The ${body.email} email address is already associated with an account. The ${user.email} email address is already associated with an account.
Try signing in with it here instead. Try signing in with it here instead.
` `
} }
); );
throw err; }
});
}); });
})
.ignoreElements();
User.observe('before save', function({ instance: user }, next) { const updateOrSave = Observable.of(ctx)
if (user) { // not new
.filter(({ isNewInstance }) => !isNewInstance)
.map(({ instance }) => instance)
// is update or save user
.filter(Boolean)
.do(user => {
// Some old accounts will not have emails associated with theme // Some old accounts will not have emails associated with theme
// we verify only if the email field is populated // we verify only if the email field is populated
if (user.email && !isEmail(user.email)) { if (user.email && !isEmail(user.email)) {
return next(createEmailError()); throw createEmailError();
} }
user.username = user.username.trim().toLowerCase(); user.username = user.username.trim().toLowerCase();
user.email = typeof user.email === 'string' ? user.email = typeof user.email === 'string' ?
user.email.trim().toLowerCase() : user.email.trim().toLowerCase() :
@ -203,14 +207,10 @@ module.exports = function(User) {
if (user.progressTimestamps.length === 0) { if (user.progressTimestamps.length === 0) {
user.progressTimestamps.push({ timestamp: Date.now() }); user.progressTimestamps.push({ timestamp: Date.now() });
} }
// this is workaround for preventing a server crash })
// we do this on save and on create .ignoreElements();
// refer strongloop/loopback/#1364 return Observable.merge(beforeCreate, updateOrSave)
if (user.password === '') { .toPromise();
user.password = null;
}
}
return next();
}); });
// remove lingering user identities before deleting user // remove lingering user identities before deleting user