Q:

How to use variable between functions

I'm having a problem of using variables between functions. As you can see down below User.username is available and good at the sign up page, but when you go to the login page I told it to first alert the value of User.username, and it alerts undefined? I'm confused here. I'm pretty sure I'm missing a concept here. Anyways Thank you so much!:
Here is a plunkr: http://plnkr.co/edit/qB3Gkeq5ji1YQyy0kpGH
Here is my script.js:
app.controller("AuthCtrl", ["$scope", "Auth","$rootScope", 
  function($scope, Auth, $rootScope) {
var User = {}

      $scope.createUser = function(username, email, password) {
        $rootScope.usernames = username
        User.username = username
        $scope.message = null;
        $scope.error = null;
    var ref2 = new Firebase("https://uniquecoders.firebaseio.com/");
  ref2.createUser({
    email: $scope.email,
    password: $scope.password
  }, function(error, userData) {
    if (error) {
      switch (error.code) {
        case "EMAIL_TAKEN":
          alert("The new user account cannot be created because the email is already in use. Try to login");
          break;
        case "INVALID_EMAIL":
          alert("The specified email is not a valid email.");
          break;
        case "INVALID_PASSWORD":
          alert("The Specified Passowrd Is not valid.")
          break;
        default:
          alert("Error creating user:", error);
      }
    } else {
      alert("Successfully created user account with username" + User.username);

      window.location.hash = "/User"
    }
  });

      };

       $scope.logIn = function(){
         alert(User.username)
       $rootScope.usernames = User.username
        $scope.message = null;
        $scope.error = null;
        var ref2 = new Firebase("https://uniquecoders.firebaseio.com/");
        ref2.authWithPassword({
          "email" : $scope.logInemail,
          "password" : $scope.logInemailpassword

        }, function(error, userData){

          if(error){
            alert("Login Failed Because : " + error)
          }
          else{
            alert("Logged In!")
            window.location.hash = "/User"
          }

        })

      }

  /*  $scope.removeUser = function() {
      $scope.message = null;
      $scope.error = null;

      Auth.$removeUser({
        email: $scope.email,
        password: $scope.password
      }).then(function() {
        $scope.message = "User removed";
      }).catch(function(error) {
        $scope.error = error;
      });
    };*/
  }
]);

A:

When page reloads the entire javascript is re executed. So user object populated in registration process is no longer available in login function because page has reloaded on clicking on signup button.
So we need to store the user name in cookie so that we can access it any time. Even when its removed from javascript runtime it will be there in the browser cache.
Its risky to store passwords in cookies for security issues.
On successful registration save the username in cookie.
Some thing like this
document.cookie ="username =" +username + "; " +"email ="+ email;

In  login function get cookie values and check if email of user is same as entered
function getCookie(name ) {
   var pairs = document.cookie.split("; "),
   count = pairs.length, parts; 
   while ( count-- ) {
    parts = pairs[count].split("=");
    if ( parts[0] === name )
        return parts[1];
}
 return false;
}

var username=getCookie("username");
var email=getCookie("username");
if($scope.logInemail ==email)
  {
     alert(username);
  }

