检查Facebook的'response.email'是否在我的数据库中

时间:2014-05-13 07:44:54

标签: javascript asp.net facebook facebook-javascript-sdk

以下代码来自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.';
    }
}

用户点击" Facebook登录"按钮并通过Facebook OAuth登录。 一旦用户登录,用户的电子邮件,姓名等的response对象就在浏览器上,很好。现在,如何检查response.email(用户的电子邮件)是否在我的数据库表用户身上?在SQL查询中使用response.email的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

首先,确保登录的范围是“电子邮件地址”

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

Testapi功能将首先获取当前用户的电子邮件地址。并且响应将包含电子邮件地址。您应该有一个服务从前端“检索”电子邮件地址,在数据库中检查电子邮件地址是否已经存在并且“返回”响应(可能就像“是”或“否”一样简单)

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

}