summaryrefslogtreecommitdiff
path: root/www/js/PortalLoginCtrl.js
blob: 2a5576235f784080e0d7c842f2404595bc71a92b (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
/* 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', function ($ionicPlatform, $scope, zm, ZMDataModel, $ionicSideMenuDelegate, $rootScope, $http, $q, $state, $ionicLoading, $ionicPopover, $ionicScrollDelegate, $ionicModal, $timeout, zmAutoLogin, $ionicHistory) {

   
    //-------------------------------------------------------------------------------
    // 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 () {
        ZMDataModel.zmDebug("Trying to unlock PIN");
        if ($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("authenticating...")
                .then(function (data) // success
                    {
                        ZMDataModel.zmDebug("PortalLogin: auth success");
                        ZMDataModel.getKeyConfigParams(1);
                        ZMDataModel.zmDebug("Transitioning state to: " + $rootScope.lastState? $rootScope.lastState:'montage');
                        $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));
                        $state.go('login');
                    });
        } 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
    //-------------------------------------------------------------------------------
    

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

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

    var loginData = ZMDataModel.getLogin();
    $scope.pinPrompt = false; // if true, then PIN is displayed else skip 

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

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


        } 
        else // no PIN Code so skip
        {

            ZMDataModel.zmDebug("PIN code not set");
            $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
            zmAutoLogin.stop(); //safety
            zmAutoLogin.start();
            zmAutoLogin.doLogin("authenticating...")
                .then(function (data) // success
                    {
                        ZMDataModel.zmDebug("PortalLogin: auth success");
                        ZMDataModel.getKeyConfigParams(1);
                        ZMDataModel.zmDebug("Transitioning state to: " + $rootScope.lastState? $rootScope.lastState:'montage');
                
                        $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));
                        $state.go('login');
                    });
        }

    } else {
        ZMDataModel.zmDebug("PortalLogin: Not logged in, so going to login");
        $state.go('login');

    }


    }]);