簡體   English   中英

對護照的Ajax調用未運行身份驗證中間件

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

這是請求身份驗證的客戶端代碼

    $("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);
      })
    });

以下是用於護照認證的節點API

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

如果進行ajax調用,Passport.authenticate函數不會調用中間件函數,但是如果頁面是使用表單提交的,則它可以正常工作。

您調用的用於身份驗證的功能是

passport.authenticate('local');

它也取決於您包含的模塊。 請在https://github.com/rupalipemare/Mongoose-Demo上檢查此處的代碼,其中有完整的示例說明護照認證。 希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM