简体   繁体   English

对护照的Ajax调用未运行身份验证中间件

[英]Ajax call to passport does not run the authenticate middleware

This is the client side code to request authentication 这是请求身份验证的客户端代码

    $("form#login-form").on("click", "#submit", function (e) {
      e.preventDefault();
      $.ajax({
        url: "/login",
        type: "POST",
        data: JSON.stringify({ email: $("#email").val(), password: $("#password").val() }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
      }).done(function (response, textStatus, jqXHR) {

        utils.clearErrors();
        location.reload();
      }).fail(function (jqXHR, textStatus, errorThrown) {

        // handle the error
        utils.handleError(jqXHR, textStatus, errorThrown);
      })
    });

Below is the node API for passport authentication 以下是用于护照认证的节点API

    app.post('/login', passport.authenticate('local-login'), (req, res, next) => {
      return res.json({ user: req.user });
    });

Passport.authenticate function does not call the middleware function in case of ajax call but if page is submitted using form then it works perfectly. 如果进行ajax调用,Passport.authenticate函数不会调用中间件函数,但是如果页面是使用表单提交的,则它可以正常工作。

the function you are u calling to authenticate is 您调用的用于身份验证的功能是

passport.authenticate('local');

It aslo depends the module you have included. 它也取决于您包含的模块。 Please check the code here on https://github.com/rupalipemare/Mongoose-Demo , wherein there is complete example demonstrating passport authentication. 请在https://github.com/rupalipemare/Mongoose-Demo上检查此处的代码,其中有完整的示例说明护照认证。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM