Res.redirect()后res.set()标头令牌丢失

时间:2015-08-12 07:52:47

标签: ajax node.js authentication express

成功登录后,我创建了一个json会话令牌,并将其作为标题添加到res对象。我重定向用户后,标头参数已丢失。我没有使用任何身份验证中间件。

我的ajax电话:

$('.form-signin').submit(function(e) {
        e.preventDefault();
        var json = {
          "email": $('#email').val(),
          "password": $('#password').val(),
        };
        $.ajax({
          url: '/api/login',
          data: json,
          type: "POST"
        })
        .fail(function(jqXHR) {
          console.log(jqXHR);
        })
        .done(function(data) {
          console.log('Back to basic');
        });
      });

我的服务器端登录路由处理程序:

app.post('/api/login', function(req, res, next) {
    pg.select().table('admins').where({ email: req.body.email })
    .catch(function(error) {
        console.error(error);
        res.redirect('/');
    })
    .then(function(user) {
        if(user) {
            bcrypt.compare(req.body.password, user[0].password, function(err, valid) {
                if (err) {
                    console.error(err);
                    res.redirect('/');
                }
                if(!valid) {
                    console.log('Password not valid');
                    res.redirect('/');
                }
                var token = jwt.sign({ email: user[0].email }, app.get('secret'), { expiresInMinutes: 60 });
                res.set('x-auth', token);
                res.redirect('/admin');
            });
        } else {
            console.log('Username not found');
            res.redirect('/');
        }
    })
});

认证功能:

var auth = function(req, res, next) {
    var token = req.body.token || req.query.token || req.headers['x-auth'];
    console.log(req.headers);

    if (token) {
        jwt.verify(token, app.get('secret'), function(err, auth) {
            if (err) {
                console.log('Token verification failure');
                res.redirect('/');
            }
            pg.select('email').from('admins').where({ email: auth.email })
            .then(function(result) {
                if(result.length) {
                    console.log('Redirected to admin');
                    next();
                } else {
                    console.log('User is not logged in');
                    res.redirect('/');
                }
            })
            .catch(function(err) {
                console.log('Database error');
                res.redirect('/');
            });
        });
    } else {
        console.log('No token');
        res.redirect('/');
    }
};

我已经添加了必要的标题来阅读我的令牌:

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'x-auth');
    next();
});

我的身份验证功能始终记录"无令牌"并且req.headers参数总是(access-control-allow标题也消失了):

{ host: 'localhost:8080',
  connection: 'keep-alive',
  accept: '*/*',
  'x-requested-with': 'XMLHttpRequest',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36',
  referer: 'http://localhost:8080/',
  'accept-encoding': 'gzip, deflate, sdch',
  'accept-language': 'et' }

现在我知道我可以使用令牌进行响应并使用第二个ajax调用将其作为数据发送,但我想处理服务器端的重定向。任何人都有一个想法,这里可能出现什么问题?

0 个答案:

没有答案