florianheinemann / passwordless

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

requestToken() never ends #117

Closed charnould closed 6 years ago

charnould commented 6 years ago

Hi,

First, thanks for your library. It works almost like a charm! 👍 But I have a small issue and I can't find a way to fix it (I'm a kind of newbie)...

Using your own example (for testing purpose), I've set my /sendtoken route, but the request never ends: the email containing the token is sent, the token works... but - using my example - I can't console.log('end here') or diplay sent.

I tried a few things without any success. It seems to miss a next(), but you do not use it on your docs.

Do you have an idea what's going on?

Thanks a lot.

const express = require('express')
const passwordless = require('passwordless')
const router = express.Router({ mergeParams: true })

var users = [
  { id: '1', email: 'myemail1@gmail.com' },
  { id: '2', email: 'myemail2@gmail.com' }
]

// POST route
router.post(
  '/sendtoken',
  function(req, res, next) {
    console.log('enter here') // <-- I can console.log() this message
    next()
  },
  passwordless.requestToken(function(user, delivery, callback) {
    console.log('inside requesttoken') // <-- I can console.log() this message
    for (var i = users.length - 1; i >= 0; i--) {
      if (users[i].email === user.toLowerCase()) {
        return callback(null, users[i].id)
      }
    }
    callback(null, null)
  }),
  function(req, res) {
    console.log('end here') // <-- I CANNOT console.log() this message
    res.sent('sent')
  }
)
olearycrew commented 6 years ago

I'm having this same problem. Is it something with an express version change or something?

imjonathanlee commented 6 years ago

I'm relatively sure it's an issue with an express version change. I used this last year with no problems, but running the same project now gives me exact same issue as OP.

florianheinemann commented 6 years ago

Hi!

I can't replicate the issue - can you post a full example? The below is the most minimal version I get together (using the latest Express) and it does work:

const express = require('express');
const app = express();
const passwordless = require('passwordless');
const MemoryStore = require('passwordless-memorystore');
const bodyParser = require('body-parser');

var users = [
    { id: 1, email: 'marc@example.com' },
    { id: 2, email: 'alice@example.com' }
];

passwordless.init(new MemoryStore());

passwordless.addDelivery(
    function(tokenToSend, uidToSend, recipient, callback) {
        console.log('Hello!\nAccess your account here: http://localhost:3000/' 
            + '?token=' + tokenToSend + '&uid=' 
            + encodeURIComponent(uidToSend));
        callback();
});

app.use(bodyParser.urlencoded({ extended: false }));
app.use(passwordless.acceptToken({ successRedirect: '/success'}));

app.get('/', (req, res) => res.send('               \
<html>                                              \
    <body>                                          \
        <h1>Login</h1>                              \
        <form action="/sendtoken" method="POST">    \
            Email:                                  \
            <br><input name="user" type="text">     \
            <br><input type="submit" value="Login"> \
        </form>                                     \
    </body>                                         \
</html>                                             \
'));

app.get('/success', (req, res) => res.send('Logged in'));

/* POST login details. */
app.post('/sendtoken', 
    passwordless.requestToken(
        function(user, delivery, callback) {
            for (var i = users.length - 1; i >= 0; i--) {
                if(users[i].email === user.toLowerCase()) {
                    return callback(null, users[i].id);
                }
            }
            callback(null, null);
        }),
        function(req, res) {
            res.send('Sent - Check your console!');
});

app.listen(3000, () => console.log('Example app listening on port 3000!'));
florianheinemann commented 6 years ago

I also added a no-config example that allows you to test passwordless in a minimal way: https://github.com/florianheinemann/passwordless/tree/master/examples/console-token

fraunos commented 6 years ago

I had the same problem - had no callback() cast at the end of delivery

AmrKafina commented 5 years ago

If anyone runs into this, make sure you call callback() at the end of your delivery method!