如果已经设置了一个,则禁止Facebook api向DB添加电子邮件

时间:2016-08-05 22:44:47

标签: php facebook facebook-graph-api laravel-5

我有一个登录系统,允许用户使用Facebook的api注册或登录。

我添加了一些代码来检查是否存在与Facebook电子邮件匹配的电子邮件,如果存在,请将Facebook ID添加到数据库中的该行。

我的帐户设置中也有一个选项,允许用户更改其电子邮件地址。

我遇到的问题是,如果用户在获得Facebook ID后更改了自己的电子邮件地址,则下次用户退出然后返回我的网站时,他们的电子邮件会更新到其Facebook帐户上的电子邮件中。

我仍然希望允许User :: createOrUpdateGraphNode($ facebook_user)仅在电子邮件不存在时才向我的数据库添加电子邮件,因此对于新用户。

但如果电子邮件已经存在,那就不行了。因此,如果用户确实更改了他们的电子邮件,则使用Facebook登录不会影响它。

我知道我可以从

中删除
protected static 

    $graph_node_field_aliases = [
            'id' => 'facebook_id',
            // 'name' => 'name',
            //'email' => 'email',
        ];

但是当新用户注册时,它不会在我的数据库中添加电子邮件。

此功能正在进行中:createOrUpdateGraphNode($facebook_user) 所以我不知道如何添加一个像这样的东西,例如:

if($user->facebook_id == facebook_id && $user->email) { 
  do something 
}

我正在使用sammyklaravel / facebooksdk https://github.com/SammyK/LaravelFacebookSdk

Facebook回调看起来像这样:

function facebookCallBack(LaravelFacebookSdk $fb) {
    // Obtain an access token.
try {
    $token = $fb->getAccessTokenFromRedirect();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    dd($e->getMessage());
}

// Access token will be null if the user denied the request
// or if someone just hit this URL outside of the OAuth flow.
if (! $token) {
    // Get the redirect helper
    $helper = $fb->getRedirectLoginHelper();

    if (! $helper->getError()) {
        abort(403, 'Unauthorized action.');
    }

    // User denied the request
    dd(
        $helper->getError(),
        $helper->getErrorCode(),
        $helper->getErrorReason(),
        $helper->getErrorDescription()
    );
}

if (! $token->isLongLived()) {
    // OAuth 2.0 client handler
    $oauth_client = $fb->getOAuth2Client();

    // Extend the access token.
    try {
        $token = $oauth_client->getLongLivedAccessToken($token);
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        dd($e->getMessage());
    }
}

$fb->setDefaultAccessToken($token);

// Save for later
Session::put('fb_user_access_token', (string) $token);

// Get basic info on the user from Facebook.
try {
    $response = $fb->get('/me?fields=id,name,email');
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    dd($e->getMessage());
}

// Convert the response to a `Facebook/GraphNodes/GraphUser` collection
$facebook_user = $response->getGraphUser();
// dd($facebook_user['email'] );


// Create the user if it does not exist or update the existing entry.
// This will only work if you've added the SyncableGraphNodeTrait to your User model.
$user = User::where('email', $facebook_user['email'])->first();

if(isset($user)) {
    // // Convert the response to a `Facebook/GraphNodes/GraphUser` collection
    $facebook_user = $response->getGraphUser();
    $current_user = $user;
    $current_user->facebook_id = $facebook_user['id'];
    $current_user->save();
} else {

$user = User::createOrUpdateGraphNode($facebook_user);

}

// Log the user into Laravel
Auth::login($user);

if (Auth::user()->terms && Auth::user()->active) {
    return redirect()->route('home');
}

$active = 1;
Auth::user()->update([
    'active' => $active,
]);

return redirect()->route('auth.signupdetails');
}

1 个答案:

答案 0 :(得分:0)

好的,我找到了一个解决方案,不是它不那么优雅,但它不坏,它有效。如果任何人可以使这更优雅或如果您有任何疑虑,请删除一行。这就是我所做的:

我替换了$user = User::createOrUpdateGraphNode($facebook_user)

if($user = User::where('email', $facebook_user['email'])->where('active', true)->first()) {
    // // Convert the response to a `Facebook/GraphNodes/GraphUser` collection
    $facebook_user = $response->getGraphUser();
    $current_user = $user;
    $current_user->facebook_id = $facebook_user['id'];
    $current_user->save();
} // if facebook id exist in databse, just update
elseif ($user = User::where('facebook_id', $facebook_user['id'])->first()) {

    $facebook_user = $response->getGraphUser();
    $current_user = $user;
    $current_user->facebook_id = $facebook_user['id'];
    $current_user->save();

} // if user facebook id and no email matching facebook's email is in the database, create new user
else {
    $facebook_user = $response->getGraphUser();

     $user = User::create([
        'email' => $facebook_user['email'],
        'facebook_id' => $facebook_user['id'],
    ]);
}

// Log the user into Laravel
Auth::login($user);