質問

The code below is from developers.facebook.com.

// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
        // Logged into your app and Facebook.
        testAPI();
    } else if (response.status === 'not_authorized') {
        // The person is logged into Facebook, but not your app.
        document.getElementById('status').innerHTML = 'Please log ' +
          'into this app.';
    } else {
        // The person is not logged into Facebook, so we're not sure if
        // they are logged into this app or not.
        document.getElementById('status').innerHTML = 'Please log ' +
          'into Facebook.';
    }
}

User clicks on "Facebook Login" button and logs in through Facebook OAuth. Once the user logs in, the response object which gives user's email, name, etc is on the browser, fine. Now, how can I check if the response.email (user's email) is on my database table Users? What's the right way to use response.email in the SQL query?

役に立ちましたか?

解決

First thing, make sure the login is with the scope "email address"

FB.login(function(response) {
   // handle the response
 }, {scope: 'email'});

Testapi function will first get the email address of the current user. And response will contain the email address. You should have a service which 'retrieves' email address from the front end, check in the db whether the email address is already existing and 'return' response (may be as simple as 'yes' or 'no')

function testApi(){
   FB.api('/me',  function(response) {
        $.ajax({
            type: 'GET',
            url: "service/isEmailExisting?email="+response.email,
            success:function(data){
             alert(data);
            }
        });
    });

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top