2017-02-24 77 views
2

有一個使用ember-simple-auth-token請求一個JWT的Ember(v2.12.0-beta.1)應用程序。使用ember-simple-auth-token的令牌請求將不包含標識字段

重要的部分發生在登錄控制器。

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, 
}; 

回答

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

相關問題