我一直在試圖做一個特定的操作,一旦我從challenge處理器收到submitAdapterAuthentication,並且我無法做任何操作,因爲我的代碼甚至沒有通過它編譯。我在我的角度服務的一種方法中使用submitAdapterAuthentication。該方法是這樣的:submitAdapterAuthentication不能正常工作
login: function (user, pass) {
//promise
var deferred = $q.defer();
//tempuser
tempUser = {username: user, password: pass};
userObj.user = user;
checkOnline().then(function (onl) {
if (onl) { //online
console.log("attempting online login");
var auth = "Basic " + window.btoa(user + ":" + pass);
var invocationData = {
parameters: [auth, user],
adapter: "SingleStepAuthAdapter",
procedure: "submitLogin"
};
ch.submitAdapterAuthentication(invocationData, {
onFailure: function (error) {
console.log("ERROR ON FAIL: ", error);
},
onConnectionFailure: function (error) {
console.log("BAD CONNECTION - OMAR", error);
},
timeout: 10000,
fromChallengeRequest: true,
onSuccess: function() {
console.log("-> submitAdapterAuthentication onSuccess!");
//update user info, as somehow isUserAuthenticated return false without it
WL.Client.updateUserInfo({
onSuccess: function() {
//return promise
deferred.resolve(true);
}
});
}
});
} else { //offline
console.log("attempting offline login");
deferred.resolve(offlineLogin());
}
uiService.hideBusyIndicator();
});
uiService.hideBusyIndicator();
return deferred.promise;
}
其中CH是 VAR CH = WL.Client.createChallengeHandler(securityTest);
和checkOnline是此功能檢查用戶是否在線與否:
function checkOnline() {
var deferred = $q.defer();
WL.Client.connect({
onSuccess: function() {
console.log("** User is online!");
deferred.resolve(true);
},
onFailure: function() {
console.log("** User is offline!");
deferred.resolve(false);
},
timeout: 1000
});
return deferred.promise;
}
最後,這是「submitLogin」的過程,我在我的SingleStepAuthAdapter.js。 SingleStepAuthAdapter是適配器的名稱。
//-- exposed methods --//
function submitLogin(auth, username){
WL.Server.setActiveUser("SingleStepAuthAdapter", null);
var input = {
method : 'get',
headers: {Authorization: auth},
path : "/",
returnedContentType : 'plain'
};
var response = "No response";
response = WL.Server.invokeHttp(input);
WL.Logger.info('Response: ' + response.isSuccessful);
WL.Logger.info('response.responseHeader: ' + response.responseHeader);
WL.Logger.info('response.statusCode: ' + response.statusCode);
if (response.isSuccessful === true && (response.statusCode === 200)){
var userIdentity = {
userId: username,
displayName: username,
attributes: {
foo: "bar"
}
};
WL.Server.setActiveUser("SingleStepAuthAdapter", userIdentity);
return {
authRequired: false
};
}
WL.Logger.error('Auth unsuccessful');
return onAuthRequired(null, "Invalid login credentials");
}
所以我想一個承諾發到我的控制器,以便將用戶重定向到另一個頁面,但作爲挑戰處理機甚至沒有工作沒有被返回的承諾。
順便說一下,我按照這個教程:https://medium.com/@papasimons/worklight-authentication-done-right-with-angularjs-768aa933329c
有誰知道這是怎麼回事?
「我的代碼甚至沒有通過它編譯」,你究竟看到了什麼症狀? – djna 2015-02-24 10:21:34
我的意思是,ch.submitAdapterAuthentication(invocationData,{})方法本身並不通過可選參數。 例如,在我的ch.submitAdapterAuthentication(invocationData,{})方法中,我傳遞了幾個函數作爲可選項以查看方法是否成功。所以我在每個可選函數中都有console.log,並且在控制檯中沒有看到任何東西。 您可以在頂部看到我的登錄方法。 – 2015-02-24 11:52:02