fix(User.confirm): Overwrite confirm w/ custom method
Reduce db calls, implement old logic
This commit is contained in:
		
				
					committed by
					
						
						mrugesh mohapatra
					
				
			
			
				
	
			
			
			
						parent
						
							6042ce2a84
						
					
				
				
					commit
					c5420229e4
				
			@@ -248,96 +248,42 @@ module.exports = function(User) {
 | 
				
			|||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  debug('setting up user hooks');
 | 
					  debug('setting up user hooks');
 | 
				
			||||||
 | 
					  // overwrite lb confirm
 | 
				
			||||||
  User.beforeRemote('confirm', function(ctx, _, next) {
 | 
					  User.confirm = function(uid, token, redirectTo) {
 | 
				
			||||||
 | 
					    return this.findById(uid)
 | 
				
			||||||
    if (!ctx.req.query) {
 | 
					      .then(user => {
 | 
				
			||||||
      return ctx.res.redirect('/');
 | 
					        if (!user) {
 | 
				
			||||||
    }
 | 
					          throw wrapHandledError(
 | 
				
			||||||
 | 
					            new Error(`User not found: ${uid}`),
 | 
				
			||||||
    const uid = ctx.req.query.uid;
 | 
					 | 
				
			||||||
    const token = ctx.req.query.token;
 | 
					 | 
				
			||||||
    const redirect = ctx.req.query.redirect;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return User.findById(uid, (err, user) => {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (err || !user || !user.newEmail) {
 | 
					 | 
				
			||||||
          ctx.req.flash('error', {
 | 
					 | 
				
			||||||
            msg: dedent`Oops, something went wrong, please try again later`
 | 
					 | 
				
			||||||
          });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
          const err = wrapHandledError(
 | 
					 | 
				
			||||||
            new Error('Theme is not valid.'),
 | 
					 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
              Type: 'info',
 | 
					              // standard oops
 | 
				
			||||||
              message: err.message
 | 
					              type: 'info',
 | 
				
			||||||
 | 
					              redirectTo
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
          return ctx.res.redirect('/');
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        if (user.verificationToken !== token) {
 | 
				
			||||||
        if (!user.verificationToken && !user.emailVerified) {
 | 
					          throw wrapHandledError(
 | 
				
			||||||
          ctx.req.flash('info', {
 | 
					            new Error(`Invalid token: ${token}`),
 | 
				
			||||||
            msg: dedent`Looks like we have your email. But you haven't
 | 
					            {
 | 
				
			||||||
             verified it yet, please sign in and request a fresh verification
 | 
					              type: 'info',
 | 
				
			||||||
             link.`
 | 
					              message: dedent`
 | 
				
			||||||
          });
 | 
					                Looks like you have clicked an invalid link.
 | 
				
			||||||
          return ctx.res.redirect(redirect);
 | 
					                Please sign in and request a fresh one.
 | 
				
			||||||
 | 
					              `,
 | 
				
			||||||
 | 
					              redirectTo
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					          );
 | 
				
			||||||
        if (!user.verificationToken && user.emailVerified) {
 | 
					 | 
				
			||||||
          ctx.req.flash('info', {
 | 
					 | 
				
			||||||
            msg: dedent`Looks like you have already verified your email.
 | 
					 | 
				
			||||||
             Please sign in to continue.`
 | 
					 | 
				
			||||||
          });
 | 
					 | 
				
			||||||
          return ctx.res.redirect(redirect);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (user.verificationToken && user.verificationToken !== token) {
 | 
					 | 
				
			||||||
          ctx.req.flash('info', {
 | 
					 | 
				
			||||||
            msg: dedent`Looks like you have clicked an invalid link.
 | 
					 | 
				
			||||||
             Please sign in and request a fresh one.`
 | 
					 | 
				
			||||||
          });
 | 
					 | 
				
			||||||
          return ctx.res.redirect(redirect);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return user.update$({
 | 
					        return user.update$({
 | 
				
			||||||
          email: user.newEmail,
 | 
					          email: user.newEmail,
 | 
				
			||||||
 | 
					          emailVerified: true,
 | 
				
			||||||
 | 
					          emailVerifyTTL: null,
 | 
				
			||||||
          newEmail: null,
 | 
					          newEmail: null,
 | 
				
			||||||
          emailVerifyTTL: null
 | 
					          verificationToken: null
 | 
				
			||||||
        })
 | 
					        }).toPromise();
 | 
				
			||||||
        .do(() => {
 | 
					 | 
				
			||||||
          return next();
 | 
					 | 
				
			||||||
        })
 | 
					 | 
				
			||||||
        .toPromise();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  User.afterRemote('confirm', function(ctx) {
 | 
					 | 
				
			||||||
    if (!ctx.req.query) {
 | 
					 | 
				
			||||||
      return ctx.res.redirect('/');
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    const redirect = ctx.req.query.redirect;
 | 
					 | 
				
			||||||
    ctx.req.flash('success', {
 | 
					 | 
				
			||||||
      msg: [
 | 
					 | 
				
			||||||
        'Your email has been confirmed!'
 | 
					 | 
				
			||||||
      ]
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
    return ctx.res.redirect(redirect);
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  User.beforeRemote('login', function(ctx, notUsed, next) {
 | 
					 | 
				
			||||||
    const { body } = ctx.req;
 | 
					 | 
				
			||||||
    if (body && typeof body.email === 'string') {
 | 
					 | 
				
			||||||
      if (!isEmail(body.email)) {
 | 
					 | 
				
			||||||
        return next(createEmailError());
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      body.email = body.email.toLowerCase();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return next();
 | 
					 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  User.afterRemote('login', function(ctx, accessToken, next) {
 | 
					  User.afterRemote('login', function(ctx, accessToken, next) {
 | 
				
			||||||
    var res = ctx.res;
 | 
					    var res = ctx.res;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user