1

I'm trying to get the device id getUUID at the time of app login along with username and password. Is the right way to get device id and post it to the server for login.

controller.js

      .controller('LoginCtrl', function($scope, $http,$rootScope,$window,$location,$cordovaDevice) {
         $scope.login = function () {
                var data ={

                  username : $scope.username,
                  password : $scope.password
                };
        document.addEventListener("deviceready", onDeviceReady, false);
            function onDeviceReady() {
               $http.post('login', data, config)
              .success(function (data, status, headers, config) {
                  // $scope.DataResponse = data;
                  $location.path('#/tab/new-order');
              })
              .error(function (data, status, header, config) {
                  $window.alert("username or password incorrect");
              });
                  console.log(device.cordova);
               }

        var config = {
            headers : {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
            }
        }
    };

  })

html code

  <form action="" class="ki-login-form" method="post" accept-charset="utf-8">
            <div class="form-group username">
                <input type="text" class="form-control" name="username" value="" id="identity" placeholder="Your Name">
            </div>
            <div class="form-group pin">
                <input type="text" class="form-control" name="password" value="" id="identity" placeholder="Your Pin">
            </div>
            <a type="submit" class="btns" ng-click='login()' >Login</a>
    </form>

After login it has to be directed to a page href="#/tab/new-order" in the ionic tab

kk19
  • 27
  • 10
  • I have added it above, please check once – kk19 Nov 14 '16 at 09:19
  • yes u can get the uuid after the device ready..and u can post it..whats ur question actually? – Lakshay Dulani Nov 14 '16 at 09:36
  • Yes I tried above method to get device id and then post it to a server but I am getting this error now $ is not defined at ChildScope.$scope.login – kk19 Nov 14 '16 at 09:41
  • i wrote an answer..see if it works..why are u using $.param..it seems to be jquery function, which i hope you are not using..and u should not use as well – Lakshay Dulani Nov 14 '16 at 09:48
  • Possible duplicate of [How to get a device identifier using Phonegap](http://stackoverflow.com/questions/20359655/how-to-get-a-device-identifier-using-phonegap) – Hardik Vaghani Nov 14 '16 at 10:11

2 Answers2

1

Add dependency injection $location in controller function like following -

 .controller('LoginCtrl', function($scope, $http, $rootScope,$window,$location) {
// Your code
});

Solution for your added comment -

Use following - Add device plugin : cordova plugin add org.apache.cordova.device

In your controller :

module.controller('MyCtrl', function($scope, $cordovaDevice) { 
    var uuid = $cordovaDevice.getUUID(); 
});
Avinash Rathod
  • 590
  • 4
  • 15
0

Change this

var data = $.param({

              username : $scope.username,
              password : $scope.password,


   });

to

var data = {

              username : $scope.username,
              password : $scope.password,


   };

UPDATE -

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log(device.cordova);
}
Lakshay Dulani
  • 1,710
  • 2
  • 18
  • 45
  • TypeError: Cannot read property 'uuid' of undefined On editing above syntax – kk19 Nov 14 '16 at 10:15
  • ionic.Platform.ready(function(){ $http.post('login', data, config) .success(function (data, status, headers, config) { // $scope.DataResponse = data; $location.path('#/tab/new-order'); }) .error(function (data, status, header, config) { $window.alert("username or password incorrect"); }); console.log( window.device.uuid ); }); – kk19 Nov 14 '16 at 10:24
  • Same uuid error after editing- check the code in the question. I've updated it – kk19 Nov 14 '16 at 10:24
  • u r still using $.param!! u shdnt do that – Lakshay Dulani Nov 14 '16 at 11:52
  • I have removed them in my code, I'll update here as well. – kk19 Nov 14 '16 at 11:53
  • instead of ionic platform ready event...do ur post on deviceready event ...see update – Lakshay Dulani Nov 14 '16 at 12:03
  • Do I need to include this also? element.innerHTML = 'Device Model: ' + device.model + '
    ' + 'Device Cordova: ' + device.cordova + '
    ' + 'Device Platform: ' + device.platform + '
    ' + 'Device UUID: ' + device.uuid + '
    ' + 'Device Version: ' + device.version + '
    '; http://stackoverflow.com/questions/20359655/how-to-get-a-device-identifier-using-phonegap
    – kk19 Nov 14 '16 at 12:23
  • glad it worked..no u need not add this code..dont forget to upvote the answer ;) – Lakshay Dulani Nov 14 '16 at 12:25
  • :) thanks all I'm not getting any error now. but this has to be there right- var uuid = $cordovaDevice.getUUID(); – kk19 Nov 14 '16 at 12:26