florianheinemann / passwordless

node.js/express module to authenticate users without password
MIT License
1.95k stars 129 forks source link

Issues to implement in a single page app #57

Closed vdcrea closed 9 years ago

vdcrea commented 9 years ago

Hi, First, thanks for this module! But i have an issue to implement it in a single page app. I used the passwordless.net source as a starting point.

As it s a single page app I need to send back to the client json instead of res.redirect.

My issue is about the /account/sendtoken route Input validation part works but failureRedirect at the end of Request token and the final function(req, res) doesn't works and I don't understand why...

Here is the complete route, any idea why server doesn't send any response after input are validated?

/* POST login screen. */
router.post('/sendtoken',
    // Input validation
    function(req, res, next) {
        req.checkBody('user', 'Please provide a valid email address').isLength(1,200).isEmail();
        req.sanitize('user').toLowerCase();
        req.sanitize('user').trim();

        var errors = req.validationErrors(true);
        if (errors) {
            console.log('There have been validation errors: ' + errors);
            req.flash('validation', errors);
//          res.redirect('../account/login');
            res.send({'reload': true});
        } else {
            next();
        }
    },
    // Request token
    passwordless.requestToken(
        function(email, delivery, callback) {
            // Return user, if he exists, create new if he doesn't
            User.findUser(email, function(error, user) {
                if(error) {
                    callback(error.toString());
                } else if(user) {
                    callback(null, user.id);
                } else {
                    User.createOrUpdateUser(email, '', '', function(error, user) {
                        if(error) {
                            callback(error.toString());
                        } else {
                            callback(null, user.id);
                        }
                    });
                }
            });
        }, { failureRedirect: '/account/response',
                failureFlash: 'We had issues sending out this email... Could you try it at a later moment?',
                successFlash: 'You should have an email in your inbox in a couple of seconds...!' }),

    function(req, res) {
//      res.redirect('/');
        res.send({'redirect': '../../'});
});

router.get('/response',function(req,res) {
    res.send({'redirect': '../../'});
});
vdcrea commented 9 years ago

Forget about it, I have integrated node mailer instead of mandrill for the delivery, and forgot to add the callback(). So it s ok I get the json response. Have a nice day.