Reputation: 13422
I have an ordering form, the user needs to signup before he/she can order, so I can create an account then process the order like this.
router.post('/project/new', passport.authenticate('local-signup'), function(req, res) {
gateway.customer.find(req.user.id, function(err, customer) {
if(customer) {
// method for a customer inside the vault
} else {
// add a non existing customer to vault
}
});
});
The problem is if someone is logged in already and wants to make an order. I need to check if they are logged in, if they are then I can proceed with the order, if they are NOT then I need to authenticate then log them in.
So something like this makes sense to me.
//prefer this!!
router.post('/project/new', function(req, res) {
if(req.user) {
// do stuff for existing logged in user
} else {
// user is NOT logged in need passport
passport.authenticate('local-signup', function(err, user, info) {
// do stuff for new user
})(req, res, next);
}
});
The problem with that is I get next is not defined
, I see in the passportjs docs Jared uses this custom callback inside a get
route.
Can someone help me make a custom callback? Does anyone have a better strategy?
Upvotes: 2
Views: 203
Reputation: 11677
It is undefined, because you haven't defined it, you can define it as such:
router.post('/project/new', function(req, res, next){
// rest of your code here
}
Though in your case, you're better off moving your authentication logic to separate middleware, something like this:
router.post('/project/new',
function(req, res, next) {
if (req.user) {
next();
} else {
// do whatever to create a new user
}
},
function(req, res) {
// this function will be called after your auth logic is done
// at this point, your req.user should be populated, respond with an error status if for some reason its not
if (!req.user) return res.send(500);
// the rest of your logic here
}
);
Upvotes: 1