【问题标题】:A token request using ember-simple-auth-token won't include the identification field使用 ember-simple-auth-token 的令牌请求将不包含标识字段
【发布时间】:2017-02-24 04:52:35
【问题描述】:

一个使用 ember-simple-auth-token 请求 JWT 的 Ember (v2.12.0-beta.1) 应用程序。

重要的部分发生在登录控制器中。

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

    // Properties
    username: 'user1',
    password: 'password123',

    // Actions
    actions: {
        login(username, password) {
            console.log('Attempting login...');

            let creds = this.getProperties('username', 'password');
            let authenticator = 'authenticator:jwt';

            this.get('session').authenticate(authenticator, creds).then(function() {
                console.log('LOGIN SUCCESS')
            }, function() {
                console.log('LOGIN FAIL')
            });
        }
    }
});

提交表单时,浏览器正在发出请求,我的后端收到了它。

问题是请求中只包含密码。请求正文的格式为{"password":"password123"},但应该看起来像{"username":"user1","password":"password123"}。当然,登录尝试失败并打印LOGIN FAIL

为什么用户名不包含在令牌请求中?

我尝试使用早期版本的 ember-simple-auth-token 和 ember-simple-auth。

这是我的配置:

ENV['ember-simple-auth'] = {
    authorizer: 'authorizer:token',
};

ENV['ember-simple-auth-token'] = {
    serverTokenEndpoint: 'http://127.0.0.1:6003/token',
    identificationField: 'username',
    passwordField: 'password',
    tokenPropertyName: 'token',
    authorizationPrefix: 'Bearer ',
    authorizationHeaderName: 'Authorization',
    refreshAccessTokens: false,
};

【问题讨论】:

    标签: ember.js ember-simple-auth


    【解决方案1】:

    ember-simple-auth-token 期望传递给authenticate 的凭据对象采用以下格式:

    {
       identification: <username>,
       password: <password>
    }
    

    所以你的代码应该是这样的:

    actions: {
        login(username, password) {
            console.log('Attempting login...');
    
            let creds = {
                identification: username,
                password: password
            };
    
            let authenticator = 'authenticator:jwt';
            this.get('session').authenticate(authenticator, creds).then(function() {
                console.log('LOGIN SUCCESS')
            }, function() {
                console.log('LOGIN FAIL')
            });
        }
    }
    

    这种情况下发送的请求是:

    {
        "password":"password123",
        "username":"user1"
    }
    

    关于这个问题有一些pull requests

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多