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:
committed by
mrugesh mohapatra
parent
d5e7bd586d
commit
e60ff3775b
@ -138,59 +138,63 @@ module.exports = function(User) {
|
||||
);
|
||||
});
|
||||
|
||||
User.beforeRemote('create', function({ req }) {
|
||||
const body = req.body;
|
||||
User.observe('before save', function(ctx) {
|
||||
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
|
||||
// this was not always the case
|
||||
if (
|
||||
typeof body.email !== 'string' ||
|
||||
!isEmail(body.email)
|
||||
typeof user.email !== 'string' ||
|
||||
!isEmail(user.email)
|
||||
) {
|
||||
return Promise.reject(createEmailError());
|
||||
throw createEmailError();
|
||||
}
|
||||
// assign random username to new users
|
||||
// actual usernames will come from github
|
||||
body.username = 'fcc' + uuid.v4();
|
||||
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;
|
||||
// use full uuid to ensure uniqueness
|
||||
user.username = 'fcc' + uuid.v4();
|
||||
|
||||
if (!user.progressTimestamps) {
|
||||
user.progressTimestamps = [];
|
||||
}
|
||||
// set email verified false on user email signup
|
||||
// should not be set with oauth signin methods
|
||||
body.emailVerified = false;
|
||||
|
||||
if (user.progressTimestamps.length === 0) {
|
||||
user.progressTimestamps.push({ timestamp: Date.now() });
|
||||
}
|
||||
return User.doesExist(null, body.email)
|
||||
.catch(err => {
|
||||
throw wrapHandledError(err, { redirectTo: '/email-signup' });
|
||||
})
|
||||
.then(exists => {
|
||||
if (!exists) {
|
||||
return null;
|
||||
}
|
||||
const err = wrapHandledError(
|
||||
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 ${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.
|
||||
`
|
||||
}
|
||||
);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
.ignoreElements();
|
||||
|
||||
User.observe('before save', function({ instance: user }, next) {
|
||||
if (user) {
|
||||
const updateOrSave = Observable.of(ctx)
|
||||
// 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
|
||||
// we verify only if the email field is populated
|
||||
if (user.email && !isEmail(user.email)) {
|
||||
return next(createEmailError());
|
||||
throw createEmailError();
|
||||
}
|
||||
|
||||
user.username = user.username.trim().toLowerCase();
|
||||
user.email = typeof user.email === 'string' ?
|
||||
user.email.trim().toLowerCase() :
|
||||
@ -203,14 +207,10 @@ module.exports = function(User) {
|
||||
if (user.progressTimestamps.length === 0) {
|
||||
user.progressTimestamps.push({ timestamp: Date.now() });
|
||||
}
|
||||
// this is workaround for preventing a server crash
|
||||
// we do this on save and on create
|
||||
// refer strongloop/loopback/#1364
|
||||
if (user.password === '') {
|
||||
user.password = null;
|
||||
}
|
||||
}
|
||||
return next();
|
||||
})
|
||||
.ignoreElements();
|
||||
return Observable.merge(beforeCreate, updateOrSave)
|
||||
.toPromise();
|
||||
});
|
||||
|
||||
// remove lingering user identities before deleting user
|
||||
|
Reference in New Issue
Block a user