passport local strategy not getting called

前端 未结 14 788
夕颜
夕颜 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: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)
    });
    

提交回复
热议问题