Passing Params from one controller to another


#1

hey everyone
I need to make an ionic app that login through authentication http web service and I finished it and it’s working , but I have t pass to the url the following (username , password , auth)
auth is either student or staff
so i need to use the user object in other places in the app (other controllers ) but i don’t know how to get the parameters again , since i can’t get it from the service because i have to pass username , password and auth to get the user object
what to do

this is my userService Code
This message was deleted

mgisApp.factory('userService', function($http) {
  var users = {};

  return {
    getUser: function(username,password,auth){

      var servicebase = "http://www.al-almanya.com/alalmanya-api/login-route-api?";
      return $http.get(servicebase + "username="+username+"&password=" + password + "&auth="+auth).then(function(response){
        //http://www.al-almanya.com/alalmanya-api/login-route-api?username=301&password=123456&auth=student
        users = response;
        return users;
      });
    },
    getSingleUser: function(uid){

      return users[uid];
    }
  }

});

and this is the controller that works well with it which is the login controller

mgisApp.controller('LoginCtrl', function($scope,$state, $stateParams, userService, $ionicPopup) {

  // An alert dialog
  $scope.showAlert = function() {
    var alertPopup = $ionicPopup.alert({
      title: 'Error',
      template: 'invalid username or password'
    });
    alertPopup.then(function(res) {
      console.log('you have accepted the alert');
    });
  }




    $scope.onSignInClicked = function(username,password,auth) {


      var inputUsername=username,
        inputPassword=password,
        inputAuth=auth;

      console.log("Input Fields:"+ inputUsername + "- "+inputPassword+ "- "+inputAuth);
      // var user = userService.getUser(inputUsername,inputPassword,inputAuth);
      userService.getUser(inputUsername,inputPassword,inputAuth).then(function(user){
        //user
        var serviceUsername=user.data.username,
          servicePassword=user.data.password,
          serviceAuth=user.data.auth;
        console.log("Service Fields:"+ serviceUsername + "- "+servicePassword+ "- "+serviceAuth);
        console.log("Service ID: "+ user.id);
        console.log(user);
        if (inputUsername == serviceUsername && inputPassword == servicePassword && inputAuth == serviceAuth) {
          localStorage.setItem('loggedIn', true);
          $state.go('tab.dash');
        } else {
          $scope.showAlert();
        }

      });

I need to get a list of messages that this particular user have

mgisApp.factory('Chats', function($http) {
  var chats = {};

  return {
    getChats: function(auth,id,api_token){
      var servicebase = "http://al-almanya.com/alalmanya-api/notifications-route-api?";
      return $http.get(servicebase + "auth="+auth+"&id=" + id + "&api_token="+api_token).then(function(response){
        // http://al-almanya.com/alalmanya-api/notifications-route-api?auth=student&id=301&api_token=sEy4nqOUxOI6FGD7ekbUElDvR0XN6AmekL2CcGxl
       chats = response;
       return chats;
      });
    }
  }
});

` and the controller I am trying to work on , trying to get the auth , id , api_token is right here

 mgisApp.controller('DashCtrl', function($scope ,userService) {
        userService.getUser(inputUsername,inputPassword,inputAuth).then(function(user){

  console.log(user.fullname);
//cant access the user because i can't have the parameters, username , password , auth
}
});

#2

Try using $rootScope, then you can pass it to another controller , or if you are going to keep the user signed in, use localStorage.
But, why do you want to store the password?


#3

Thanks @vahidvdn for your answer

I am already using local storage ,I used it to keep the user logged in ,but I don’t need to store the password or use it in any of the other controllers,all what i need to use is the id and auth and api token from the userService , because i want to integrate with messages service , the messages api take in the url ,
examplewebsite.com?id=value&api_token=y&auth=student

and those (id , api_token , auth ) are in the userService , and i need to get the current one