summaryrefslogtreecommitdiff
path: root/www/js/PortalLoginCtrl.js
blob: 0ef20705b0ee2a1e8cf314a06b9ad369a09f9d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* jshint -W041 */
/* jshint -W083 */
/*This is for the loop closure I am using in line 143 */
/* jslint browser: true*/
/* global vis,cordova,StatusBar,angular,console,moment */
angular.module('zmApp.controllers').controller('zmApp.PortalLoginCtrl', ['$ionicPlatform', '$scope', 'zm', 'ZMDataModel', '$ionicSideMenuDelegate', '$rootScope', '$http', '$q', '$state', '$ionicLoading', '$ionicPopover', '$ionicScrollDelegate', '$ionicModal', '$timeout', 'zmAutoLogin', '$ionicHistory', '$cordovaTouchID',  'EventServer', function ($ionicPlatform, $scope, zm, ZMDataModel, $ionicSideMenuDelegate, $rootScope, $http, $q, $state, $ionicLoading, $ionicPopover, $ionicScrollDelegate, $ionicModal, $timeout, zmAutoLogin, $ionicHistory, $cordovaTouchID,  EventServer) {


    $scope.$on('$ionicView.enter',
        function () {

            ZMDataModel.zmDebug("Inside Portal login Enter handler");


            $ionicHistory.nextViewOptions({
                disableBack: true
            });

            $scope.pindata = {};
            if ($ionicSideMenuDelegate.isOpen()) {
                $ionicSideMenuDelegate.toggleLeft();
                ZMDataModel.zmDebug("Sliding menu close");
            }


            $scope.pinPrompt = false; // if true, then PIN is displayed else skip 

            if (ZMDataModel.isLoggedIn()) {
                ZMDataModel.zmLog("User credentials are provided");
                
                

                // You can login either via touch ID or typing in your code     
                if ($ionicPlatform.is('ios') && loginData.usePin) {
                    $cordovaTouchID.checkSupport()
                        .then(function () {
                            // success, TouchID supported
                            $cordovaTouchID.authenticate("")
                                .then(function () {
                                        ZMDataModel.zmLog("Touch Success");
                                        // Don't assign pin as it may be alphanum
                                        unlock(true);

                                    },
                                    function () {
                                        ZMDataModel.zmLog("Touch Failed");
                                    });
                        }, function (error) {
                            ZMDataModel.zmLog("TouchID not supported");
                        });
                } else {
                    ZMDataModel.zmLog("not checking for touchID");
                }

                if (loginData.usePin) {
                    $scope.pinPrompt = true;


                } else // no PIN Code so skip
                {

                    // don't get stuck in this state
                    // will happen if you switch to background in portal state
                    if ($rootScope.lastState == "zm-portal-login") {
                        ZMDataModel.zmDebug("Last state was portal-login, so forcing montage");
                        $rootScope.lastState = "montage";
                    }
                    ZMDataModel.zmDebug("PIN code not set");
                    $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
                    zmAutoLogin.stop(); //safety
                    zmAutoLogin.start();
                    zmAutoLogin.doLogin("<button class='button button-clear' style='line-height: normal; min-height: 0; min-width: 0;' ng-click='$root.cancelAuth()'><i class='ion-close-circled'></i>&nbsp;authenticating...</button>")
                        .then(function (data) // success
                        {
                            ZMDataModel.zmDebug("PortalLogin: auth success");
                            ZMDataModel.getKeyConfigParams(1);
                            ZMDataModel.zmDebug("Transitioning state to: " + 
                                                $rootScope.lastState ? $rootScope.lastState : 'montage');

                            ZMDataModel.getAPIversion()
                            .then (function(data) {
                                ZMDataModel.zmLog("Got API version: " + data);
                                var ld = ZMDataModel.getLogin();
                                 if (versionCompare(data,zm.minAppVersion)==-1 && data !="0.0.0")
                                //if (versionCompare(data,zm.minAppVersion))
                                {

                                    $state.go('lowversion', {"ver":data});
                                }
                                
                                if (versionCompare(data,zm.recommendedAppVersion)==-1 && data !="0.0.0")
                                //if (versionCompare(data,zm.minAppVersion))
                                {

                                    $state.go('importantmessage', {"ver":data});
                                }
                            });
                            EventServer.refresh();

                            if ($rootScope.tappedNotification)
                            {
                                var ld = ZMDataModel.getLogin();

                                //console.log ("***** NOTIFICATION TAPPED  ");
                                $rootScope.tappedNotification = 0;
                                $ionicHistory.nextViewOptions({disableBack: true});	

                                if (ld.onTapScreen == 'montage' )
                                {
                                    ZMDataModel.zmDebug("Going to montage");
                                    $state.go("montage", {}, { reload: true });

                                    return;
                                }
                                else
                                {
                                    ZMDataModel.zmDebug("Going to events");
                                    $state.go("events", {"id": 0}, { reload: true });
                                    return;
                                }
                            }

                                $state.go($rootScope.lastState ? $rootScope.lastState : 'montage', $rootScope.lastStateParam);
                        },
                            // coming here means auth error
                            // so go back to login
                        function (error) {
                            ZMDataModel.zmDebug("PortalLogin: error authenticating " +
                                JSON.stringify(error));
                            if (!$rootScope.userCancelledAuth)
                            {
                                    $ionicHistory.nextViewOptions({
                                        disableAnimate: true,
                                    disableBack: true
                                    });
                                    $state.go('login');
                            }
                            else   
                            {
                                // do this only once - rest for next time
                                $rootScope.userCancelledAuth = false;
                            }
                        });
                }

            } else {
                ZMDataModel.zmDebug("PortalLogin: Not logged in, so going to login");
                if (ZMDataModel.isFirstUse())
                {
                    ZMDataModel.zmDebug ("First use, showing warm and fuzzy...");
                    $ionicHistory.nextViewOptions({
                        disableAnimate: true,
                        disableBack: true
                    });
                    $state.go('first-use');
                }
                else
                {
                    if (!$rootScope.userCancelledAuth)
                    {
                            $ionicHistory.nextViewOptions({
                                disableAnimate: true,
                            disableBack: true
                            });
                            $state.go('login');
                    }
                    else   
                    {
                        // do this only once - rest for next time
                        $rootScope.userCancelledAuth = false;
                    }
                }
            }

        });

    //-------------------------------------------------------------------------------
    // remove status is pin is empty
    //-------------------------------------------------------------------------------

    $scope.pinChange = function () {
        if ($scope.pindata.pin == null) {
            $scope.pindata.status = "";
        }
    };

    //-------------------------------------------------------------------------------
    // unlock app if PIN is correct
    //-------------------------------------------------------------------------------
    $scope.unlock = function () {
        unlock(false);
    };
    
    //credit: https://gist.github.com/alexey-bass/1115557
    function versionCompare(left, right) {
        if (typeof left + typeof right != 'stringstring')
            return false;

        var a = left.split('.');
        var  b = right.split('.');
        var i = 0;
        var len = Math.max(a.length, b.length);

        for (; i < len; i++) {
            if ((a[i] && !b[i] && parseInt(a[i]) > 0) || (parseInt(a[i]) > parseInt(b[i]))) {
                return 1;
            } else if ((b[i] && !a[i] && parseInt(b[i]) > 0) || (parseInt(a[i]) < parseInt(b[i]))) {
                return -1;
            }
        }

        return 0;
}
    
    

    function unlock(touchVerified) {
        ZMDataModel.zmDebug("Trying to unlock PIN");
        if (touchVerified || ($scope.pindata.pin == loginData.pinCode)) {
            ZMDataModel.zmDebug("PIN code entered is correct");
            $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
            zmAutoLogin.stop(); //safety
            zmAutoLogin.start();
            zmAutoLogin.doLogin("<button class='button button-clear' style='line-height: normal; min-height: 0; min-width: 0;' ng-click='$root.cancelAuth()'><i class='ion-close-circled'></i>&nbsp;authenticating...</button>")
                .then(function (data) // success
                    {
                        EventServer.refresh();
                        // don't get stuck in this state
                        // will happen if you switch to background in portal state
                        if ($rootScope.lastState == "zm-portal-login") {
                            ZMDataModel.zmDebug("Last state was portal-login, so forcing montage");
                            $rootScope.lastState = "montage";
                        }
                        ZMDataModel.zmDebug("PortalLogin: auth success");
                          ZMDataModel.getAPIversion()
                                .then (function(data) {
                                    var ld = ZMDataModel.getLogin();
                                    ZMDataModel.zmLog("Got API version: " + data);
                                    if ((versionCompare(data,zm.minAppVersion)==-1) && (ld.url.indexOf("arjunrc.") == -1) && data !="0.0.0")
                                    {
                                       
                                      
                                        $state.go('lowversion', {"ver":data});
                                    }
                                    
                              
                                });
                        ZMDataModel.getKeyConfigParams(1);
                
                        if ($rootScope.tappedNotification)
                        {
                            $rootScope.tappedNotification = 0;
                            $ionicHistory.nextViewOptions({disableBack: true});		
                               console.log ("***** NOTIFICATION TAPPED GOING TO EVENTS ");
                            $state.go("events", {"id": 0}, { reload: true });
                            return;
                            
                        }
                
                        if ($rootScope.lastState == 'lowversion') $rootScope.lastState = 'montage';
                        ZMDataModel.zmDebug("Transitioning state to: " + $rootScope.lastState ? $rootScope.lastState : 'montage');
                        console.log ("*********** GOING TO " + $rootScope.lastState);
                        $state.go($rootScope.lastState ? $rootScope.lastState : 'montage', $rootScope.lastStateParam);
                    },
                    // coming here means auth error
                    // so go back to login
                    function (error) {
                        ZMDataModel.zmDebug("PortalLogin: error authenticating " +
                            JSON.stringify(error));
                        if (!$rootScope.userCancelledAuth)
                        {
                                $ionicHistory.nextViewOptions({
                                    disableAnimate: true,
                                disableBack: true
                                });
                                $state.go('login');
                        }
                        else   
                        {
                            // do this only once - rest for next time
                            $rootScope.userCancelledAuth = false;
                        }
                    });
        } else {
            $scope.pindata.status = "Invalid PIN";

            // wobble the input box on error
            var element = angular.element(document.getElementById("pin-box"));

            element.addClass("animated shake")
                .one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',
                    function () {
                        element.removeClass("animated shake");
                    });
        }
    }

    //-------------------------------------------------------------------------------
    // Controller Main
    //-------------------------------------------------------------------------------
    console.log("************* ENTERING PORTAL MAIN ");
    var loginData = ZMDataModel.getLogin();
     $ionicSideMenuDelegate.canDragContent(false);

        

    }]);