Log in with FaceBook

3

I am trying to make the connection with FaceBook from my web application but it is not working, previously it worked correctly.

This is the way I declare the script in the parent template so to speak, since I'm using the model MVC of PHP

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'id_app',
      cookie     : true,
      xfbml      : true,
      version    : 'v3.0'
    });

    FB.AppEvents.logPageView();   

  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "https://connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

and I send everything to the JS, which is called FacebookFile.js, where I post the following

/*=============================================
BOTÓN FACEBOOK
=============================================*/

$(".facebook").click(function(){

    FB.login(function(response){

        validarUsuario();

    }, {scope: 'public_profile, email'})

})

/*=============================================
VALIDAR EL INGRESO
=============================================*/

function validarUsuario(){

    FB.getLoginStatus(function(response){

        statusChangeCallback(response);

    })

}

/*=============================================
VALIDAMOS EL CAMBIO DE ESTADO EN FACEBOOK
=============================================*/

function statusChangeCallback(response){

    if(response.status === 'connected'){

        testApi();

    }else{
        console.log("response", response);
        swal({
          title: "¡ERROR!",
          text: "¡Ocurrió un error al ingresar con Facebook, vuelve a intentarlo!",
          type: "error",
          confirmButtonText: "Cerrar",
          closeOnConfirm: false
        },

        function(isConfirm){
            if (isConfirm) {    
                window.location = localStorage.getItem("rutaActual");
            } 
        });

    }

}

/*=============================================
INGRESAMOS A LA API DE FACEBOOK
=============================================*/

function testApi(){

    FB.api('/me?fields=id,name,email,picture',function(response){

        if(response.email == null){

            swal({
              title: "¡ERROR!",
              text: "¡Para poder ingresar al sistema debe proporcionar la información del correo electrónico!",
              type: "error",
              confirmButtonText: "Cerrar",
              closeOnConfirm: false
            },

            function(isConfirm){
                if (isConfirm) {    
                    window.location = localStorage.getItem("rutaActual");
                } 
            });

        }else{

            var email = response.email;
            var nombre = response.name;
            var foto = "http://graph.facebook.com/"+response.id+"/picture?type=large";

            var datos = new FormData();
            datos.append("email", email);
            datos.append("nombre",nombre);
            datos.append("foto",foto);

            $.ajax({

                url:rutaOculta+"ajax/usuarios.ajax.php",
                method:"POST",
                data:datos,
                cache:false,
                contentType:false,
                processData:false,
                success:function(respuesta){

                    if(respuesta == "ok"){

                        window.location = localStorage.getItem("rutaActual");

                    }else{

                        swal({
                          title: "¡ERROR!",
                          text: "¡El correo electrónico "+email+" ya está registrado con un método diferente a Facebook!",
                          type: "error",
                          confirmButtonText: "Cerrar",
                          closeOnConfirm: false
                        },

                        function(isConfirm){
                            if (isConfirm) {    

                             FB.getLoginStatus(function(response){

                                 if(response.status === 'connected'){     

                                    FB.logout(function(response){

                                        deleteCookie("fblo_300012410734892");

                                        setTimeout(function(){

                                            window.location=rutaOculta+"salir";

                                        },500)

                                    });

                                    function deleteCookie(name){

                                         document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';

                                    }

                                 }

                             })

                            } 
                        });

                    }

                }

            })

        }

    })

}

/*=============================================
SALIR DE FACEBOOK
=============================================*/

$(".salir").click(function(e){

    e.preventDefault();

     FB.getLoginStatus(function(response){

         if(response.status === 'connected'){     

            FB.logout(function(response){

                deleteCookie("fblo_300012410734892");

                console.log("salir");

                setTimeout(function(){

                    window.location=rutaOculta+"salir";

                },500)

            });

            function deleteCookie(name){

                 document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';

            }

         }

     })

})

but in the function statusChangeCallback does not enter the response of "connected" and immediately goes to the error message and in the console I get a status unknow

    
asked by Cesar Gutierrez Davalos 25.07.2018 в 00:59
source

1 answer

0

This did not happen to me with FB but with other API'S

The common problem was that chrome settings were changed to block cookies and third-party data. Simply, disable this setting and it works.

Chrome - > Configuration - > Advanced Configuration - > Privacy and security - > Content settings - > Cookies - > Uncheck "Block third-party cookies"

You could check it with chrome with that configuration.

In case this is correct, you should do the same with your other browsers.

    
answered by 25.07.2018 в 01:19