function autoLogin(successCallback, errorCallback) {
    //repopulate the user and the client with the info from the userSession
    var token = Usergrid.userSession.getAccessToken();
    Usergrid.ApiClient.setToken(token);

    runManagementQuery(new Usergrid.Query("GET","users/" + Usergrid.userSession.getUserEmail(), null, null,
      function(response) {
        if (!response) {
          errorCallback();
          return
        }

        //get the orgname and app name from the url
        var requestedOrgName = Usergrid.Navigation.router.getOrgNameFromURL();
        var requestedAppName = Usergrid.Navigation.router.getAppNameFromURL();

        var orgNameSelected = false;
        var appNameSelected = false;

        //store all the organizations and their applications
        for (org in response.data.organizations) {
          //grab the name
          var orgName = response.data.organizations[org].name;
          //grab the uuid
          var orgUUID = response.data.organizations[org].uuid;
          organization = new Usergrid.Organization(orgName, orgUUID);

          if (orgName === requestedOrgName) {
            Usergrid.ApiClient.setOrganizationName(orgName);
            orgNameSelected = true;
          }

          for (app in response.data.organizations[org].applications) {
            //grab the name
            var appName = app.split("/")[1];
            if (!appName) { appName = app; }
            if (appName === requestedAppName && orgNameSelected && orgName === requestedOrgName) {
              Usergrid.ApiClient.setApplicationName(appName);
              appNameSelected = true;
            }

            //grab the id
            var appUUID = response.data.organizations[org].applications[app];
            //store in the new Application object
            application = new Usergrid.Application(appName, appUUID);
            organization.addItem(application);
          }
          //add organization to organizations list
          Usergrid.organizations.addItem(organization);
        }

        if (!orgNameSelected) {
          //select the first org by default
          var firstOrg = Usergrid.organizations.getFirstItem();
          //save the first org in the client
          Usergrid.ApiClient.setOrganizationName(firstOrg.getName());
        }

        //store user data in local storage
        Usergrid.userSession.saveAll(response.data.uuid, response.data.email, response.data.token);

        //store the token in the client
        Usergrid.ApiClient.setToken(response.data.token);

        if (successCallback) {
          successCallback(response);
        }
      },
      function(response) {
        if (response == 'API CALL TIMEOUT') {
          showMessagePage();
        } else if (response == 'auth_bad_access_token' || response == 'expired_token') {
          logout();
        } else {
          displayLoginError();
        }
      }
    ));
    return;
  }
/**
   *  Reauthenticate an admin who already has a token
   *  @method autoLogin
   *  @params {function} successCallback - callback function for success
   *  @params {function} errorCallback - callback function for error
   */
Reauthenticate an admin who already has a token
@method autoLogin
@params {function} successCallback - callback function for success
@params {function} errorCallback - callback function for error
Reauthenticate an admin who already has a token @method autoLogin @params { function } successCallback - callback function for success @params { function } errorCallback - callback function for error
function autoLogin(successCallback, errorCallback) {
    var token = Usergrid.userSession.getAccessToken();
    Usergrid.ApiClient.setToken(token);
    runManagementQuery(new Usergrid.Query("GET","users/" + Usergrid.userSession.getUserEmail(), null, null,
      function(response) {
        if (!response) {
          errorCallback();
          return
        }
        var requestedOrgName = Usergrid.Navigation.router.getOrgNameFromURL();
        var requestedAppName = Usergrid.Navigation.router.getAppNameFromURL();
        var orgNameSelected = false;
        var appNameSelected = false;
        for (org in response.data.organizations) {
          var orgName = response.data.organizations[org].name;
          var orgUUID = response.data.organizations[org].uuid;
          organization = new Usergrid.Organization(orgName, orgUUID);
          if (orgName === requestedOrgName) {
            Usergrid.ApiClient.setOrganizationName(orgName);
            orgNameSelected = true;
          }
          for (app in response.data.organizations[org].applications) {
            var appName = app.split("/")[1];
            if (!appName) { appName = app; }
            if (appName === requestedAppName && orgNameSelected && orgName === requestedOrgName) {
              Usergrid.ApiClient.setApplicationName(appName);
              appNameSelected = true;
            }
            var appUUID = response.data.organizations[org].applications[app];
            application = new Usergrid.Application(appName, appUUID);
            organization.addItem(application);
          }
          Usergrid.organizations.addItem(organization);
        }
        if (!orgNameSelected) {
          var firstOrg = Usergrid.organizations.getFirstItem();
          Usergrid.ApiClient.setOrganizationName(firstOrg.getName());
        }
        Usergrid.userSession.saveAll(response.data.uuid, response.data.email, response.data.token);
        Usergrid.ApiClient.setToken(response.data.token);
        if (successCallback) {
          successCallback(response);
        }
      },
      function(response) {
        if (response == 'API CALL TIMEOUT') {
          showMessagePage();
        } else if (response == 'auth_bad_access_token' || response == 'expired_token') {
          logout();
        } else {
          displayLoginError();
        }
      }
    ));
    return;
  }
function autoLogin ( successCallback , errorCallback ) { var token = Usergrid . userSession . getAccessToken ( ) ; Usergrid . ApiClient . setToken ( token ) ; runManagementQuery ( new Usergrid . Query ( "GET" , "users/" + Usergrid . userSession . getUserEmail ( ) , null , null , function ( response ) { if ( ! response ) { errorCallback ( ) ; return } var requestedOrgName = Usergrid . Navigation . router . getOrgNameFromURL ( ) ; var requestedAppName = Usergrid . Navigation . router . getAppNameFromURL ( ) ; var orgNameSelected = false ; var appNameSelected = false ; for ( org in response . data . organizations ) { var orgName = response . data . organizations [ org ] . name ; var orgUUID = response . data . organizations [ org ] . uuid ; organization = new Usergrid . Organization ( orgName , orgUUID ) ; if ( orgName === requestedOrgName ) { Usergrid . ApiClient . setOrganizationName ( orgName ) ; orgNameSelected = true ; } for ( app in response . data . organizations [ org ] . applications ) { var appName = app . split ( "/" ) [ 1 ] ; if ( ! appName ) { appName = app ; } if ( appName === requestedAppName && orgNameSelected && orgName === requestedOrgName ) { Usergrid . ApiClient . setApplicationName ( appName ) ; appNameSelected = true ; } var appUUID = response . data . organizations [ org ] . applications [ app ] ; application = new Usergrid . Application ( appName , appUUID ) ; organization . addItem ( application ) ; } Usergrid . organizations . addItem ( organization ) ; } if ( ! orgNameSelected ) { var firstOrg = Usergrid . organizations . getFirstItem ( ) ; Usergrid . ApiClient . setOrganizationName ( firstOrg . getName ( ) ) ; } Usergrid . userSession . saveAll ( response . data . uuid , response . data . email , response . data . token ) ; Usergrid . ApiClient . setToken ( response . data . token ) ; if ( successCallback ) { successCallback ( response ) ; } } , function ( response ) { if ( response == 'API CALL TIMEOUT' ) { showMessagePage ( ) ; } else if ( response == 'auth_bad_access_token' || response == 'expired_token' ) { logout ( ) ; } else { displayLoginError ( ) ; } } ) ) ; return ; }
Reauthenticate an admin who already has a token
  @method autoLogin
  @params {function} successCallback - callback function for success
  @params {function} errorCallback - callback function for error
Reauthenticate an admin who already has a token @method autoLogin @params { function } successCallback - callback function for success @params { function } errorCallback - callback function for error
//repopulate the user and the client with the info from the userSession //get the orgname and app name from the url //store all the organizations and their applications //grab the name //grab the uuid //grab the name //grab the id //store in the new Application object //add organization to organizations list //select the first org by default //save the first org in the client //store user data in local storage //store the token in the client