Make the js-lib to work with maas mobile flow (#5)
* Make the js-lib to work with maas mobile flow
The library now react on received data from the server ( based on status) and propagete when status
changes to the client, so appropriate message can be shown to the user
* Fix some namings
Rename cb2 to cbStatus to better represent what is used for
Fix variable name to meet naming convention
diff --git a/lib/mpin.js b/lib/mpin.js
index bbbed18..1ee8159 100644
--- a/lib/mpin.js
+++ b/lib/mpin.js
@@ -616,7 +616,7 @@
});
};
- Mpin.prototype.waitForMobileAuth = function (timeoutSeconds, requestSeconds, cb) {
+ Mpin.prototype.waitForMobileAuth = function (timeoutSeconds, requestSeconds, cb, cbStatus) {
var self = this, _reqData = {};
if (!this.webOTT) {
return cb({code: Errors.wrongFlow.code, type: Errors.wrongFlow.type, message: "Need to call getAccessNumber method before this."}, null);
@@ -624,6 +624,7 @@
return cb({code: Errors.missingParams.code, type: Errors.missingParams.type, message: "Missing timeout/expiration period(in seconds)."}, null);
}
+ self.mobileStatus = self.mobileStatus || '';
if (typeof this.timeoutPeriod === "undefined") {
this.timeoutPeriod = timeoutSeconds * 1000;
@@ -635,22 +636,34 @@
this.request(_reqData, function (err, data) {
var _requestPeriod;
- if (err) {
- if (err.status === 401 && self.timeoutPeriod > 0) {
- _requestPeriod = requestSeconds ? requestSeconds * 1000 : 3000;
- self.timeoutPeriod -= _requestPeriod;
- self.intervalID2 = setTimeout(function () {
- self.waitForMobileAuth.call(self, timeoutSeconds, requestSeconds, cb);
- }, _requestPeriod);
- return;
- } else if (self.timeoutPeriod <= 0) {
- delete self.timeoutPeriod;
- cb && cb(Errors.timeoutFinish, null);
- return;
- }
+ if (err) {
+ cb && cb(err, null);
} else {
- self._authenticate({mpinResponse: data}, cb);
+ authOTT = data.authOTT
+ delete data.authOTT
+
+ if(data.status === 'authenticate') {
+ cbStatus && cbStatus(data);
+ self._authenticate({mpinResponse: {authOTT: authOTT}}, cb);
+ } else {
+ if (self.timeoutPeriod > 0) {
+ _requestPeriod = requestSeconds ? requestSeconds * 1000 : 3000;
+ self.timeoutPeriod -= _requestPeriod;
+ if (data.status !== self.mobileStatus) {
+ self.mobileStatus = data.status;
+ cbStatus && cbStatus(data);
+ }
+ self.intervalID2 = setTimeout(function () {
+ self.waitForMobileAuth.call(self, timeoutSeconds, requestSeconds, cb, cbStatus);
+ }, _requestPeriod);
+ return;
+ } else if (self.timeoutPeriod <= 0) {
+ delete self.timeoutPeriod;
+ cb && cb(Errors.timeoutFinish, null);
+ return;
+ }
+ }
}
});
};