skip to Main Content

I managed to login via facebook api (I think). This is my code:

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId            : '134824563795810',
      autoLogAppEvents : true,
      xfbml            : true,
      version          : 'v2.10'
    });
    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 = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

   function myFacebookLogin() {
    FB.login(function(){}, {scope: 'publish_actions'});
    }

</script>

And simple button just to test if this works:

<button onclick="myFacebookLogin()">Kontynuuj przez facebook</button>

After clicking on it I receive this pop up:

enter image description here

I click OK, and I think I’m logged in. Another click generates small popup windows, that disappear quickly. I said I think I’m logged in, because I don’t use any authorization on my website. I don’t need it.

My question is: if I’m logged in, how can I get the user’s first and last name and put this values into my bracket input?

enter image description here

I want to make script only put values into inputs. Like:

  • Imię -> first name
  • Nazwisko -> last name

Any suggestions?

EDIT:

I finally came up with this code:

<script>
function getUserData() {
        FB.api('/me?fields=first_name,last_name,email,location', function(response) {
                var imie = document.getElementById('basketInputName');
                if(!imie || !imie.value)
                    imie.value = response.first_name;
                var nazwisko = document.getElementById('basketInputLastName');
                    if(!nazwisko || !nazwisko.value)
                        nazwisko.value = response.last_name;
                var email = document.getElementById('basketInputMail');
                    if(!email || !email.value)
                        email.value = response.email;
                var location = document.getElementById('cityToHidden');
                    if(!location || !location.value)
                        location.value = response.location.name;
        });
}
  window.fbAsyncInit = function() {


FB.init({
  appId            : '134824563795810',
  autoLogAppEvents : true,
  xfbml            : true,
  version          : 'v2.10'
});
FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
                //user is authorized
                document.getElementById('loginBtn').style.display = 'none';
                getUserData();
        } else {
                //user is not authorized
        }
});
  };

  (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/en_US/sdk.js";
 fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

   function myFacebookLogin() {
FB.login(function(){
    getUserData();
}, {scope: 'user_location'});
}

But it’s not working how i would like. When i click loggin button nothing happens, but when i refresh page, and check bucket i see that firstName, lastName and email are in inputs. And i don’t know how to make dynamicly fill inputs by clicking on login button without refreshing page.
Can somebody help?

2

Answers


  1. use user_about_me & public_profile scope

    function myFacebookLogin() {
        FB.login(function(){}, {scope: 'publish_actions,user_about_me,public_profile'});
    }
    
    Login or Signup to reply.
  2. use public_profile scope

    function myFacebookLogin() {
    FB.login(function(){}, {scope: 'publish_actions,public_profile'});
    

    }

    In public profile you get first_name and last_name

    See here details

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search