Unauthorized : CSRF validation failed issue for Logged in user I need to hide button for Logged in user. For that I have written the following code in dashboard.js file.

Code working fine for anonymous user. But not working for Logged in user.

$(document).on("pageshow","#user_login",function(){
      try {
            $.ajax({
              url: "example.com/?q=drupalgap/system/connect.json",
              type: 'post',
              dataType: 'json',
              error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('page_dashboard - failed to system connect');
                console.log(JSON.stringify(XMLHttpRequest));
                console.log(JSON.stringify(textStatus));
                console.log(JSON.stringify(errorThrown));
              },
              success: function (data) {
                var drupal_user = data.user;
                //console.log(data);
                if (drupal_user.uid == 0) { // user is not logged in, show the login button, hide the logout button
                alert(drupal_user.uid);
                }
                else { // user is logged in, hide the login button, show the logout button
                  alert(drupal_user.uid);
                }
              }
            }); 

      } //try end
      catch (error) { alert("page_dashboard - " + error); }
    });


For logged in user getting popup like : .
Added X-CSRF-Token in beforeSend like below everything works fine now.

$(document).on("pageshow","#user_login",function(){
      try {

        $.ajax({
              url:"example.com/?q=services/session/token",
              type:"get",
              dataType:"text",
              error:function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
              },
              success: function (token) {        

                        $.ajax({
                          url: "http://192.168.0.6/site_name/?q=drupalgap/system/connect.json",
                          type: 'post',
                          dataType: 'json',
                          beforeSend: function (request) {
                                request.setRequestHeader("X-CSRF-Token", token);
                            },
                          error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert('page_dashboard - failed to system connect');
                            console.log(JSON.stringify(XMLHttpRequest));
                            console.log(JSON.stringify(textStatus));
                            console.log(JSON.stringify(errorThrown));
                          },
                          success: function (data) {
                            var drupal_user = data.user;
                            //console.log(data);
                            if (drupal_user.uid == 0) { // user is not logged in, show the login button, hide the logout button
                            //alert(drupal_user.uid);
                             // $('#button_login').show();
                             // $('#edit-user-login-form-name').hide();
                            }
                            else { // user is logged in, hide the login button, show the logout button
                              //$('#edit-user-login-form-pass').hide();
                              alert(drupal_user.uid);
                             //$('#button_logout').show();
                            }
                          }
                        });

                    }
        });


      } //try end
      catch (error) { alert("page_dashboard - " + error); }
    });