EmberJS - Ember 简单的 Auth Oath2 'grant _type' 未定义

EmberJS - Ember Simple Auth Oath2 'grant _type' not defined

我一直在关注 Ember 可用的简单身份验证演练 here。我已经按照说明添加了各种代码片段,但是当我提交登录表单时,我收到 'grant_type' not defined 错误。

这是当前设置:

// Login Form
        <form {{action 'authenticate' on='submit'}}>
          <label for="identification">Login</label> {{input value=identification placeholder='Enter Login' class='form-control'}}
          <br>
          <label for="password">Password</label> {{input value=password placeholder='Enter Password' class='form-control' type='password'}}
          <br>
          <button type="submit" class="btn btn-default">Login</button>
        </form>

        {{#if errorMessage}}
          <p>
            <strong>Login failed: </strong>
            <code>{{errorMessage}}</code>
          </p>
        {{/if}}

//index.js controller

import Controller from '@ember/controller';

export default Controller.extend({
  session: Ember.inject.service('session'),

  actions: {
    invalidateSession() {
      this.get('session').invalidate();
    },
    authenticate() {
      let {identification, password } = this.getProperties('identification', 'password');
      this.get('session').authenticate('authenticator:oath2', identification, password).catch((reason) => {this.set('errorMessage', reason.error)
    })
    }
  }

});

//application route

import Route from '@ember/routing/route';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin);
    // authenticators/oath.js

import OAuth2PasswordGrantAuthenticator from 'ember-simple-auth/authenticators/oauth2-password-grant';

export default OAuth2PasswordGrantAuthenticator.extend({
  serverTokenEndpoint: 'http://server:port/api/token',


});

//api endpoint

var tokenRouter = express.Router();

tokenRouter.post('/api/token', function(req, res) {


  if (req.body.grant_type === 'password') {
    if (req.body.username === 'letme' && req.body.password === 'in') {
      res.status(200).send('{"access_token": "secret token!"}');
    } else {
      res.status(400).send('{ "error": invalid_grant_type" }')
    }
    } else {
      res.status(400).send(' { "error": "unsupported_grant_type" }')
    }

})

app.use('/', tokenRouter)

请求已成功发送到我的端点,并生成 500 错误,并显示 grant_type 未定义的消息。查看请求,似乎也没有发送用户名或密码。

据我所知,我的代码与文档和补充视频中的代码相同,但我显然遗漏了一些东西。

我终于想通了。

在 Chrome 调试工具的“网络”选项卡中检查表单数据后,很明显 grant_type 已正确发送。

我的问题是缺少了一些 body-parser 中间件:

app.use(bodyParser.urlencoded({ extended: false }))