Combine two functions into one Stripe and Passport

Viewed 80

I currently have it set so a user signs up with just their email and password. Simple and easy, I want them to be able to select a plan from a select element. I know how to access the part of the request I want using req.body.plan if the name for the select is plan.

I have two separate controller functions currently to do this in a bit different way. I have it so the postSignup signs the user up using passport.js and everything is all good. I also have postPlan that when the user is on /billing and selects a plan then submits the form that they have that plan assigned to them. The part I am stuck on is the User.findById(req.user.id, function(err, user) { part, more specifically the req.user.id. How can I take the two functions below and have them combined so that is sets the user's plan based off the select element in the signup form.

postPlan

exports.postPlan = function(req, res, next){
  var plan = req.body.plan;
  console.log("plan: ",req.body.plan);
  var stripeToken = null;

  if(plan){
    plan = plan.toLowerCase();
  }

  if(req.user.stripe.plan == plan){
    req.flash('info', {msg: 'The selected plan is the same as the current plan.'});
    return res.redirect(req.redirect.success);
  }

  if(req.body.stripeToken){
    stripeToken = req.body.stripeToken;
  }

  if(!req.user.stripe.last4 && !req.body.stripeToken){

    req.flash('errors', {msg: 'Please add a card to your account before choosing a plan.'});
    return res.redirect(req.redirect.failure);
  }

  User.findById(req.user.id, function(err, user) {
    if (err) return next(err);

    user.setPlan(plan, stripeToken, function (err) {
      var msg;

      if (err) {
        if(err.code && err.code == 'card_declined'){
          msg = 'Your card was declined. Please provide a valid card.';
        } else if(err && err.message) {
          msg = err.message;
        } else {
          msg = 'An unexpected error occurred.';
        }

        req.flash('errors', { msg:  msg});
        return res.redirect(req.redirect.failure);
      }
      req.flash('success', { msg: 'Plan has been updated.' });
      res.redirect(req.redirect.success);
    });
  });
};

postSignup

    exports.postSignup = function(req, res, next){
      req.assert('email', 'Please sign up with a valid email.').isEmail();
      req.assert('password', 'Password must be at least 6 characters long').len(6);

      var errors = req.validationErrors();

      if (errors) {
        req.flash('errors', errors);
        req.flash('form', {
          email: req.body.email
        });
        return res.redirect('/signup');
      }
      // calls next middleware to authenticate with passport
      passport.authenticate('signup', {
        successRedirect: '/dashboard', // Select redirect for post signup
        failureRedirect: '/signup',
        failureFlash : true
      })(req, res, next);
        next();
    };

Current Idea

    exports.postSignup = function(req, res, next){
  req.assert('email', 'Please sign up with a valid email.').isEmail();
  req.assert('password', 'Password must be at least 6 characters long').len(6);

  var errors = req.validationErrors();

  if (errors) {
    req.flash('errors', errors);
    req.flash('form', {
      email: req.body.email
    });
    return res.redirect('/signup');
  }
  // calls next middleware to authenticate with passport
  passport.authenticate('signup', {
    successRedirect: '/dashboard', // Select redirect for post signup
    failureRedirect: '/signup',
    failureFlash : true
  });
  // var plan = req.body.plan;
  var plan = 'silver';
  var stripeToken = null;
  if(req.body.stripeToken){
    stripeToken = req.body.stripeToken;
  }
  User.findById({"email":req.body.email}, function(err, user) {
    if (err) return next(err);
    console.log("Ran here");
    user.setPlan(plan, stripeToken, function (err) {
      var msg;
      if (err) {
        if(err && err.message) {
          msg = err.message;
        } else {
          msg = 'An unexpected error occurred.';
        }
        req.flash('errors', { msg:  msg});
        return res.redirect(req.redirect.failure);
      }
      req.flash('success', { msg: 'Thanks for signing up! ' });
      res.redirect(req.redirect.success);
    });
  });

  (req, res, next);
    next();
};

Error message for current attempt above

{
  "message": "Cast to ObjectId failed for value \"{ email: 'snappierjaguar@gmail.com' }\" at path \"_id\" for model \"User\"",
  "error": {
    "message": "Cast to ObjectId failed for value \"{ email: 'snappierjaguar@gmail.com' }\" at path \"_id\" for model \"User\"",
    "name": "CastError",
    "stringValue": "\"{ email: 'snappierjaguar@gmail.com' }\"",
    "kind": "ObjectId",
    "value": {
      "email": "snappierjaguar@gmail.com"
    },
    "path": "_id"
  }
}
0 Answers
Related