summaryrefslogtreecommitdiff
path: root/www/js/PortalLoginCtrl.js
blob: 11f7543b874e7d629d2cfebce40876540255acf1 (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
/* 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) {
    
    // Main
    
    
    $ionicHistory.nextViewOptions({
    disableBack: true
  });
    
    $scope.pindata = {};
    if ($ionicSideMenuDelegate.isOpen())
    {
        $ionicSideMenuDelegate.toggleLeft();
        ZMDataModel.zmDebug("Sliding menu close");
    }
    
    $scope.pinChange = function()
    {
        
        console.log ($scope.pindata.pin);
        if ($scope.pindata.pin == null)
        {
            console.log ("Empty");
            $scope.pindata.status="";
        }
    };
    
    $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);
                        $state.go('montage');
                },
                       // 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";
        }
    };
    
    var loginData = ZMDataModel.getLogin();
    $scope.pinPrompt = false;

    if (ZMDataModel.isLoggedIn()) {
        ZMDataModel.zmLog ("User credentials are provided");
        
        if (loginData.usePin)
        {
            $scope.pinPrompt = true;
            
            
        }
        else{
            
            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);
                        $state.go('montage');
                },
                       // 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');
        
    }
    
    
    }]);