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