Hi,
I am facing a cache problem in the
client side
authentication method.
I use a nice navigation panel on my webapp. It works well except when I need to log into my webapp.
This is my method :
5 files :
- index.html (or whatever.html) : index page including index.js and navpanel.js
- index.js (or whatever.js)
: ajax methods
- auth.html : authentication page including auth.js
- auth.js : ajax method wich redirect you on index.html if authentication is true
- navpanel.js : ajax method wich display in a panel menu the links you need to use (it's not a really security issue, I check it on server side)
I don't really understand why, but i
t seems that because navpanel is loaded everytime those functions are already in the DOM.
when I need to call renderPanel() after the authentication form process (wich works), I am redirected to index.html but my navigation panel is empty. I have to refresh my page and the navigation panel is displayed properly.
Could someone explain me why and how can I correct this ?
I tried to disable ajax-cache and others things but with no results...
Thx guys
This is my index.html page :
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title lang="en">Wiseflat - home</title>
- <link rel="stylesheet" href="css/themes/default/jquery.mobile-1.3.2.min.css">
- <link rel="stylesheet" href="_assets/css/jqm-demos.css">
- <!--<link rel="stylesheet" href="_assets/css/google-fonts.css">-->
- <link rel="shortcut icon" href="favicon.ico">
- <script src="js/jquery.js"></script>
- <script src="js/webx/jquery.validate.js"></script>
- <script src="js/webx/additional-methods.js"></script>
- <script src="js/jquery.mobile-1.3.2.min.js"></script>
- <script src="js/jquery-lang.js" charset="utf-8" type="text/javascript"></script>
- <script src="js/langpack/fr.js" charset="utf-8" type="text/javascript"></script>
- <script type="text/javascript">
- window.lang = new jquery_lang_js();
- $().ready(function () {
- window.lang.run();
- });
- </script>
- </head>
- <body>
- <div data-role="page" class="jqm-demos jqm-demos-index" id="index">
- <div data-role="header" class="jqm-header">
- <h1 class="jqm-logo"><a href="index.html" data-ajax="false" id="nav_getIndex"><img src="_assets/img/jquery-logo.png" alt="Wiseflat"></a></h1>
- <a href="#nav-panel" class="jqm-navmenu-link" data-icon="bars" data-iconpos="notext" lang="en">Navigation</a>
- </div><!-- /header -->
- <div data-role="content" class="jqm-content">
- <h4 lang="en">Weather</h4>
- <div id="index_debug"></div>
- <div class="jqm-list">
- <ul data-role="listview" data-theme="d" data-inset="true" id="index_weather"></ul>
- </div>
- <h4 lang="en">Scenarios</h4>
- <div class="jqm-list">
- <ul data-role="listview" data-theme="d" data-inset="true" id="index_scenarios"></ul>
- </div>
- <!--<h4 lang="en">Last events</h4>
- <div class="jqm-list">
- <ul data-role="listview" data-theme="d" data-inset="true" id="index_events"></ul>
- </div>-->
- </div><!-- /content -->
- <div data-role="footer" class="jqm-footer">
- <p class="jqm-version"></p>
- <p>Copyright 2012 Wiseflat</p>
- </div><!-- /footer -->
- <div data-role="panel" class="jqm-nav-panel jqm-navmenu-panel" data-position-fixed="true" data-position="left" data-display="reveal" data-theme="c" id="nav-panel">
- <ul data-role="listview" id="nav_panel_list" data-theme="d" data-divider-theme="d" data-icon="false" data-global-nav="demos" class="jqm-list"></ul>
- </div><!-- /panel -->
- <script src="js/webx/navpanel.js"></script>
- <script src="js/webx/index.js"></script>
- </div>
- </body>
- </html>
This is an exemple of my ajax POST/PUT/GET methods from index.js :
- function findAll() {
- $.ajax({
- type: 'GET',
- url: rootURL + '/all',
- dataType: "json",
- success: function(data){
- jsondata = data;
- if(jsondata.auth == false){
- $.mobile.changePage("auth.html");
- }else {
- renderList(jsondata);
- renderPanel(jsondata);
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- debug('error', 'List unsuccessfully fetched');
- }
- });
- }
if data.auth ==false, I redirect to auth.html
This is the main part of my auth.html page :
- <div data-role="page" class="jqm-demos jqm-demos-index" id="locations">
- <div data-role="content" class="jqm-content">
- <div class="jqm-list">
- <form id="auth_form">
- <p>Login: <input type="text" name="auth_login" id="auth_login" /></p>
- <p>Password: <input type="password" name="auth_password" id="auth_password" /></p>
- <div id="demo_container" >
- <button data-mini="true" id="auth_btnLogin" data-mini="true" lang="en">Login</button>
- </div>
- </form>
- </div><!-- /content -->
- <br />
- <div id="auth_debug"></div>
- <br />
- <div data-role="footer" class="jqm-footer"></div><!-- /footer -->
- <script src="js/webx/auth.js"></script>
- </div><!-- /page -->
This is my auth.js file :
- var rootURL = "/webx/api";
- var debug_mode=0;
- $(document).on('click', '#auth_btnLogin', function(e){
- e.stopImmediatePropagation();
- $("#auth_form").validate({
- errorLabelContainer: "#auth_debug",
- rules: {
- auth_login: {
- required: true,
- textOnly: true
- }
- },
- submitHandler: function(form){
- auth();
- return false;
- }
- });
- });
- function auth() {
- //debug('debug', 'auth()');
- $.ajax({
- type: 'POST',
- cache: false,
- contentType: 'application/json',
- url: rootURL + '/login',
- dataType: "json",
- data: formToJSON(),
- success: function(data, textStatus, jqXHR){
- if(data.auth == false){
- $.mobile.changePage("auth.html");
- }
- else {
- $.mobile.changePage("index.html", {reloadPage:false,changeHash:false});
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- //debug('error', 'Category unsuccessfully added');
- }
- });
- }
- function formToJSON() {
- return JSON.stringify({
- "login": $('#auth_login').val(),
- "password": $('#auth_password').val()
- });
- }
And the last one : navpanel.js : renderpanel()
- function renderPanel(data) {
- $('#nav_panel_list li').remove();
- $('#nav_panel_list').append('<li data-role="list-divider" lang="en">Navigation</li>');
- $('#nav_panel_list').append('<li><a href="indexWeather.html" data-ajax="false" id="nav_getIndexWeather" lang="en">Weather</a></li>');
- $('#nav_panel_list').append('<li><a href="indexDevices.html" data-ajax="false" id="nav_getIndexDevices" lang="en">Devices</a></li>');
- $('#nav_panel_list').append('<li><a href="indexAreas.html" data-ajax="false" id="nav_getIndexAreas" lang="en">Areas</a></li>');
- $('#nav_panel_list').append('<li><a href="indexScenarios.html" data-ajax="false" id="nav_getIndexScenarios" lang="en">Scenarios</a></li>');
- $('#nav_panel_list').append('<li><a href="indexEvents.html" data-ajax="false" id="nav_getIndexEvents" lang="en">Events</a></li>');
- if(data.group > 0){
- $('#nav_panel_list').append('<li><a href="#" data-ajax="false" id="nav_getLogout" lang="en">Logout</a></li>');
- }
- else {
- $('#nav_panel_list').append('<li data-role="list-divider" lang="en">Logged as '+data.login+'</li>');
- $('#nav_panel_list').append('<li><a href="auth.html" data-ajax="false" id="nav_getLogin" lang="en">Sign in</a></li>');
- }
- if(data.group > 1){
- $('#nav_panel_list').append('<li data-role="list-divider" lang="en">Settings</li>');
- $('#nav_panel_list').append('<li><a href="users.html" data-ajax="false" id="nav_getUsers" lang="en">Users</a></li>');
- $('#nav_panel_list').append('<li><a href="areas.html" data-ajax="false" id="nav_getAreas" lang="en">Areas</a></li>');
- $('#nav_panel_list').append('<li><a href="locations.html" data-ajax="false" id="nav_getLocation" lang="en">Locations</a></li>');
- $('#nav_panel_list').append('<li><a href="categories.html" data-ajax="false" id="nav_getCategories" lang="en">Categories</a></li>');
- $('#nav_panel_list').append('<li><a href="devices.html" data-ajax="false" id="nav_getDevices" lang="en">Devices</a></li>');
- $('#nav_panel_list').append('<li><a href="scenarios.html" data-ajax="false" id="nav_getScenarios" lang="en">Scenarios</a></li>');
- $('#nav_panel_list').append('<li><a href="network.html" data-ajax="false" id="nav_getNetwork" lang="en">Network</a></li>');
- $('#nav_panel_list').append('<li data-role="list-divider" lang="en">Plugins</li>');
- $('#nav_panel_list').append('<li><a href="sms.html" data-ajax="false" id="nav_getSms" lang="en">Sms</a></li>');
- $('#nav_panel_list').append('<li><a href="scripts.html" data-ajax="false" id="nav_getPlugins" lang="en">Scripts</a></li>');
- $('#nav_panel_list').append('<li><a href="services.html" data-ajax="false" id="nav_getServices">Services</a></li>');
- }
- $('#nav_panel_list').append('<li data-role="list-divider" lang="en">About</li>');
- $('#nav_panel_list').append('<li><a href="about.html" data-ajax="false" id="index_about" id="nav_getAbout" lang="en">me</a></li>');
- $('#nav_panel_list').listview('refresh');
- $('#nav_panel_list').trigger('create');
- window.lang.change(data.lang);
- window.lang.run(data.lang);
- }