passport local strategy not getting called

前端 未结 14 799
夕颜
夕颜 2020-12-13 04:31

I\'m sure I\'m missing something really obvious here, but I can\'t figure this out. The function I\'ve passed to the LocalStrategy constructor doesn\'t get called when the l

相关标签:
14条回答
  • 2020-12-13 04:55

    My problem was, that I had the enctype='multipart/form-data'. Just change that to multipart='urlencoded'. And I think that will solve it.

    0 讨论(0)
  • 2020-12-13 04:58

    You get 401 when you using different username, password input fields than the default ones. You have to provide that in your LocalStrategy like this :

    passport.use(new LocalStrategy({
        usernameField: 'login',
        passwordField: 'password'
      },
    
      function(username, password, done) {
      ...
      }
    ));
    

    Default is username and password I think. See the docs here.

    0 讨论(0)
  • 2020-12-13 04:58

    It's possible your request wasn't formatted properly (particularly the body of it) and your username and password weren't being sent when you thought they were.

    Here is an example of an API call wrapper that enforces your request body is parsed as json:

    Api = {};
    Api.request = (route, options) => {
      options = options || {};
    
      options.method = options.method || 'GET';
      options.credentials = 'include';
    
      if (options.method === 'POST') {
        options.headers = {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        };
        options.body = JSON.stringify(options.data) || '{}';
      }
    
      fetch(absoluteUrl + '/api/' + route, options)
        .then((response) => response.json())
        .then(options.cb || (() => {}))
        .catch(function(error) {
          console.log(error);
        });
    };
    

    It can be used this way:

    Api.request('login', {
      data: {
        username: this.state.username,
        password: this.state.password
      },
      method: 'POST',
      cb: proxy((user) => {
        console.log(user);
      }, this)
    });
    
    0 讨论(0)
  • 2020-12-13 04:59

    For Express 4.x:

    // Body parser
    var bodyParser = require('body-parser');
    
    app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
    app.use(bodyParser.json()) // parse application/json
    

    c.f. https://github.com/expressjs/body-parser

    0 讨论(0)
  • 2020-12-13 04:59
    1. npm install passport-local

    2. var passport = require('passport') , LocalStrategy = require('passport-local').Strategy;

    According to passportjs.org and it worked for me!

    0 讨论(0)
  • 2020-12-13 05:02

    For me everything was setup properly.. The issue was that I was collecting form data and then creating a json from form data and send it like JSON.stringify(formdatajson) to server. (For me login form was a popup on screen)

    I Found my mistake by debugging passportjs...

    If you also have same problem and none of the above solution seems working for you then debug passportjs.

    open

    strategy.js
    

    put debugger in below method.

    Strategy.prototype.authenticate 
    

    Check what form data is coming for you. Hope this help ...

    0 讨论(0)
提交回复
热议问题