Facebook login web javascript

2

I have a problem and it took a couple of days without finding a solution, I want to put the facebook login button on my website and I have managed to get the email but only in the development phase when I put the application in real no longer captures me the email, you only get the email from the developer accounts and I need to be able to catch the email in order to make a user registration when you log in for the first time with your facebook account, any help is well received, thanks!

    function statusChangeCallback(response)
    {
        console.log('statusChangeCallback');
        console.log(response);

        if (response.status === 'connected')
        {
            testAPI();
        }
        else if (response.status === 'not_authorized')
        {
            document.getElementById('status').innerHTML = 'Inicia sesión';
        }
        else
        {
        document.getElementById('status').innerHTML = 'Inicia sesión';
        }
    }

    function checkLoginState()
    {
        FB.getLoginStatus(function(response){
            statusChangeCallback(response);
        });
    }

    window.fbAsyncInit = function()
    {
        FB.init({
            appId      : 'xxxxxxxxxxxxxxxx',
            cookie     : true,  // enable cookies to allow the server to access 
            // the session
            xfbml      : true,  // parse social plugins on this page
            version    : 'v2.5' // use version 2.5
        });
    };

    function login_fb()
    {
        FB.login(function(response){  
            validarUsuario();  
        }, {scope: 'public_profile, email'});  
    }

    function validarUsuario()
    {  
        FB.getLoginStatus(function(response) {  
            if(response.status == 'connected') {
                testAPI();
            } else if(response.status == 'not_authorized') {  
                alert('Debes autorizar la app!');  
            } else {  
                alert('Debes ingresar a tu cuenta de Facebook!');  
            }  
        });  
    }

    // Load the SDK asynchronously
    (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 = "//connect.facebook.net/es_ES/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));

    function testAPI() {
        console.log('Bienvenido ');
        FB.api('/me?fields=name,email,first_name,last_name', function(response) {
            console.log('Este es el email: ' + response.email);
        });
    }
    
asked by luisinho 21.03.2016 в 14:27
source

1 answer

1

I understand you follow this document in your implementation

Facebook Login for the Web with the JavaScript SDK

I imagine that the user with whom you authenticate the first time is in cache that is why it always takes the same authentication

What happens if you implement a logout

Facebook Javascript SDK: Basic Login and Logout example

    function logout() {
        FB.logout(function(response) {
          // user is now logged out
        });
    }

so that the site returns to ask you the credentials, but validates that it redirects to the facebook site, you enter the credentials of the person and redirect to your site.

In your implementation I see the login, but not the logout so if the user is in cache always use their credentials giving good authentication, with a logout forces to change user.

> > to log in, the authorization sale opens and I see the permissions you request (email and public profile) I accept, but it does not return the email but if I open facebook I appear logged

Analyzing the api

Graph API Request

I can see that the parameters do not assign them correctly, try using something like

FB.api('/me', 'get', { access_token: token, fields: 'id,name,mail' },  function(response) {
       console.log(response);
 });

Note: you can try passing or not the token

    
answered by 21.03.2016 в 15:13