在我的Ember應用中,我有一個登錄操作。海市蜃樓驗證失敗請求(401未授權)總是在ember-simple-auth-token中解析
login: function(emailAddress, password) {
this.get('session').authenticate('authenticator:jwt', {
'identification': emailAddress,
'password': password
}).catch((reason) => {
console.log('Reason: ' + JSON.stringify(reason));
this.set('errorMessage', 'Login failed.');
});
}
當我測試我的服務器(http://127.0.0.1:8091),它工作正常。當響應爲200時,我已登錄。如果是401,則會設置我的錯誤消息。
這個問題發生在ember-cli-mirage。我有這樣的代碼來進行測試:
this.post('http://127.0.0.1:8091', function(db, req) {
let body = JSON.parse(req.requestBody);
if (body.handler === '[email protected]!' && body.password === 'secretpassword') {
return {
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFiYzEyMyJ9.X0O3xMVikn-5l9gXBU5a2XF6vlMmTzm4mCqUNA68e-A',
id: 'abc123'
};
} else {
return new Response(401, {}, {});
}
});
我只是給測試令牌,當憑證正確它的工作原理。我可以登錄到我的應用程序。但是,如果我試圖用錯誤的憑據,然後我在JS控制檯收到此錯誤:
Uncaught TypeError: Cannot read property 'split' of undefined
調查的一點點後,似乎這就是所謂在這段代碼的功能getTokenData內發生的錯誤:
authenticate: function authenticate(credentials, headers) {
var _this2 = this;
return new _ember['default'].RSVP.Promise(function (resolve, reject) {
var data = _this2.getAuthenticateData(credentials);
_this2.makeRequest(_this2.serverTokenEndpoint, data, headers).then(function (response) {
_ember['default'].run(function() {
var token = _ember['default'].get(response, _this2.tokenPropertyName);
var tokenData = _this2.getTokenData(token);
var expiresAt = _ember['default'].get(tokenData, _this2.tokenExpireName);
var tokenExpireData = {};
_this2.scheduleAccessTokenRefresh(expiresAt, token);
tokenExpireData[_this2.tokenExpireName] = expiresAt;
response = _ember['default'].merge(response, tokenExpireData);
resolve(_this2.getResponseData(response));
});
}, function (xhr) {
_ember['default'].run(function() {
reject(xhr.responseJSON || xhr.responseText);
});
});
});
},
看到它所在的行var tokenData = _this2.getTokenData(token);
。自請求被拒絕後,是否應該調用第二個函數(被拒絕的承諾)?
它看起來像ember-simple-auth-token試圖使用一個不存在的令牌,因爲響應是一個空的401.我試圖包括一個令牌,即使在拒絕的請求中,但我仍然得到同樣的錯誤。
請記住,當響應來自我的本地服務器(甚至我的真實服務器),它的工作正常。
我找不到調試海市蜃樓的方法。我怎樣才能看到有關答覆的細節?請求不會從我的瀏覽器中顯示在我的開發人員工具的網絡選項卡下。
接下來我應該着手解決這個問題?