ngCordova SQLite


#1

Hi all,

I’m using SQLite to store credentials data before using SQLite i used to session storage (it’s not persist).
I got success when create table and insert some data, but when i try to select data by init my app return blank, there is nothing error or log cause i used Xcode to run this (iOS)
here my code

//app.js

'use strict';

var db = null;

angular.module('app', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $cordovaSQLite, DSCacheFactory) {
  $ionicPlatform.ready(function() {              
    //SQLite database
    db = $cordovaSQLite.openDB({ name: "my.db" });
    //create tables
    db.transaction(function(tx) {
      //create session table
      tx.executeSql('CREATE TABLE IF NOT EXISTS session (id integer primary key, name varchar(50), value text)');

      }, function(e) {
        console.log("ERROR: " + e.message);
    });
  });

})

here my auth service
//auth.js

'use strict';
angular.module('logiseraApp').service('auth', function($http, $q, $cordovaSQLite, $window, Constants){
	var userdata = {};
	var logged_in = false;

	this.init = function() {
		//check from table session
	    var query = "SELECT value from session where name = ?";
        
        $cordovaSQLite.execute(db, query, ["userdata"]).then(function(res) {
            if(res.rows.length > 0){
                userdata = JSON.parse(res.rows.item(0).value);
            }
        }, function (err) {
            console.error(err);
        });
            
	}

this.setUserdata = function(data) {
		var userdataJSON = JSON.stringify(data);
		//$window.sessionStorage["userdata"] = userdataJSON;

		//insert session data into session table
		var query = "INSERT INTO session (name, value) VALUES (?,?)";
	    $cordovaSQLite.execute(db, query, ["userdata", userdataJSON]).then(function(res) {
	      console.log("data was stored: " + res.insertId);
	    }, function (err) {
	      console.error(err);
	    });

		userdata = data;
	}
	this.init();
})

Thank you all,


#2

Done,

I solved it by my self, actually it’s not error by ng-cordova sqlite or my sintax of sqlite/ng-cordova is wrong it’s return blank because i check $state every request
i add auth.init() in this method and i remove this.init() in auth service

.run(function($state, $rootScope, $ionicLoading, auth) {
      $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){
        auth.init();
        $ionicLoading.show({
          template: 'Loading...'
        });

        if(!toState.authenticate && auth.isLoggedIn()){
          // User is logged in redirect to profile page
          $state.transitionTo('app.profile');
          event.preventDefault(); 
        }else if(toState.authenticate && !auth.isLoggedIn()){
          // User isn’t authenticated
          $state.transitionTo("login");
          $ionicLoading.hide();
          event.preventDefault(); 
        }
      });

      $rootScope.$on("$stateChangeSuccess", function(event, toState, toParams, fromState, fromParams){
        $ionicLoading.hide();
      });
});

#3

hey I am making an ionic app that has a registration page and want to take input from user on the registration page and authenticate the entered credentials on the login page. please can you help me out as in how to go about this process. thank you.


#4

See this examples with Angular services: https://github.com/jdnichollsc/Ionic-Starter-Template

Regards, Nicholls