$('#page_dashboard').on('pageshow',function(){ try{ $.ajax({ url:"http://your_url/services/session/token", dataType:"text", error:function (jqXHR, textStatus, errorThrown) { alert(jqXHR); alert(textStatus); }, success: function (token) { // Call system connect with session token. $.ajax({ url: 'http://your_url/drupal/endpoint/system/connect.json', type: "post", dataType: "json", beforeSend: function (request) { request.setRequestHeader("X-CSRF-Token", token); }, error: function (jqXHR, textStatus, errorThrown) { alert(jqXHR); alert(textStatus); // alert(errorThrown); }, success: function (data) { var drupal_user = data.user; if (drupal_user.uid == 0) { // user is not logged in, show the login button, hide the logout button $('#button_login').show(); $('#button_logout').hide(); } else { // user is logged in, hide the login button, show the logout button $('#button_login').hide(); $('#button_logout').show(); } } }); } }); } catch(error) { alert("page_dashboard - " + error)}; }); $(document).on("click",'#button_logout',function(){ try { $.ajax({ url:"http://your_url/services/session/token", type:"get", dataType:"text", error:function (jqXHR, textStatus, errorThrown) { alert(jqXHR); alert(textStatus); // alert(errorThrown); }, success: function (token) { // Call system connect with session token. $.ajax({ url: "http://your_url/endpoint/user/logout.json", type: 'post', dataType: 'json', beforeSend: function (request) { request.setRequestHeader("X-CSRF-Token", token); }, error: function (jqXHR, textStatus, errorThrown) { alert('button_logout - failed to logout'); alert(JSON.stringify(jqXHR)); alert(JSON.stringify(textStatus)); alert(JSON.stringify(errorThrown)); }, success: function (data) { alert("You have been logged out."); $.mobile.changePage("index.html",{reloadPage:true},{allowSamePageTranstion:true},{transition:'none'}); } }); } }); } catch (error) { alert("button_logout - " + error); } });