Test user
Test user

Reputation: 459

how to call remember me in ionic?

Hi i am creating an app using ionic side-menu in that i need to remember the logged in user for that i am saving the username and password like this.

window.localStorage['rem'] = 'yes';
window.localStorage['Username'] = $scope.loginData.username;
window.localStorage['password'] =$scope.loginData.password;

The saving function is working fine, problem is i tried to read the saved data while starting the app controller is not calling.How can i call the controller while loading the login page in ionic side menu app?

Thanks in advance

Upvotes: 1

Views: 5304

Answers (3)

Badharudeen
Badharudeen

Reputation: 1

onSubmit() {
debugger;
this.submitted = true;
this.toastService.presentLoading();
if(this.item.id === '' && this.item.password === '' && this.rememberMe){
  this.item.id  = JSON.parse(localStorage.getItem('ID'));
  this.item.password =  JSON.parse(JSON.stringify(localStorage.getItem('PW')));
}
if (!this.validate()) {
  this.toastLoginFail('Please enter Username and / or Password.');
  this.toastService.dismissLoader();
  return;
}

this.loginService.login(this.item).subscribe(
  response => {
    if(this.rememberMe){
    localStorage.setItem('ID',this.item.id);
    localStorage.setItem('PW',this.item.password);
    }
    this.toastService.dismissLoader();
    this.clearControl();
    this.initializeMenu();
    this.sqlite.DeleteAllDepotEntry();
    const menuNavigation = response.result.FkRoleId;
    if (
      menuNavigation === EnumRolesService.DepotSorter ||
      menuNavigation === EnumRolesService.DepotSupervisor
    ) {
      this.navCtrl.navigateForward(CONST_URL.depotReceipt);
    } else {
      this.navCtrl.navigateForward(CONST_URL.parcelScan);
    }
  },
  error => {
    this.toastService.dismissLoader();
    this.toastLoginFail('Invalid Username and / or Password.');
  }
);

}

Upvotes: -1

iDeekshith
iDeekshith

Reputation: 433

To save the data in local storage

window.localStorage.setItem ("username",$scope.loginData.username);
window.localStorage.setItem("password", $scope.loginData.password);

To retrieve data use

window.localStorage.getItem("username");
window.localStorage.getItem("password");

Now create a deviceready event listener inside $ionicPlatform.ready function.

check the sample code 



$ionicPlatform.ready(function() {                
            document.addEventListener("deviceready", function()
                                           {
                                            if(window.localstorage.getItem("username")! == null && window.localstorage.getItem("password")! == null){
                                             state.go("afterLogin.html");
                                            }
                                            else
                                                 state.go("login.html");

                                            }, false);
                                 }

Upvotes: 6

Reena
Reena

Reputation: 1119

Try this:

angular.module('ionic.utils', [])

.factory('$localstorage', ['$window', function($window) {
  return {
    set: function(key, value) {
      $window.localStorage[key] = value;
    },
    get: function(key, defaultValue) {
      return $window.localStorage[key] || defaultValue;
    },
    setObject: function(key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    },
    getObject: function(key) {
      return JSON.parse($window.localStorage[key] || '{}');
    }
  }
}]);

Upvotes: 0

Related Questions