Offline login caching

This sample code demonstrates how to achieve offline login caching in an answerSpace.

Script Tag in Logout Interaction Message
goBackToHome();

if (navigator && navigator.bgcachereset && navigator.onLine) {

    navigator.bgcachereset.resetCache();

}

 

Script Tag inside HTML Head
$(document).ready(function() {

      var oldRequestLoginStatus = requestLoginStatus;

      var newRequestLoginStatus = function() {

 

        if (window && window.device) {

 

          var deferred = new $.Deferred();

 

          if(navigator.connection.type === Connection.NONE || navigator.connection.type === Connection.UNKNOWN){

 

            var deferred = new $.Deferred();

              //get the account details from device storage

              $.when(MyAnswers.store.get('offline_loggedinUser'), MyAnswers.store.get('offline_isLoggedIn'))

                .done(function(value, val2) { 

                                                       var loggedinUser = JSON.parse(value);

                                                       var isLoggedIn = JSON.parse(val2);

 

                                                       if (loggedinUser != null) {

                                                         MyAnswers.loginAccount = loggedinUser;

                                                         MyAnswers.isLoggedIn = isLoggedIn;

                                                       }

 

                                                       updateLoginButtons();

                                                       deferred.resolve();

                                                     });

          }

          else {

            $.ajax({

              url: siteVars.serverAppPath + '/xhr/GetLogin.php',

              dataType: 'json',

              complete: function(xhr, xhrStatus) {

                if (isAJAXError(xhrStatus) || xhr.status !== 200) {

                  deferred.reject();

                  return;

                }

                var data = $.parseJSON(xhr.responseText);

                if (data) {

                  if (data.status === 'LOGGED IN') {

                    MyAnswers.loginAccount = data;

                    MyAnswers.isLoggedIn = true;

                    //store the cached response

                    MyAnswers.store.set('offline_loggedinUser', JSON.stringify(data));

                    MyAnswers.store.set('offline_isLoggedIn', JSON.stringify(true));

                  } else {

                    MyAnswers.isLoggedIn = false;

                    delete MyAnswers.loginAccount;

                    //store the cached response

                    MyAnswers.store.remove('offline_loggedinUser');

                    MyAnswers.store.remove('offline_isLoggedIn');

                  }

                }

                updateLoginButtons();

                deferred.resolve();

              },

              timeout: Math.max(currentConfig.downloadTimeout * 1000, computeTimeout(500))

            });

          }

          return deferred.promise();

        }

 

        return oldRequestLoginStatus();

      };

 

      requestLoginStatus = newRequestLoginStatus;

    });