Laravel 5.1&社交网站 - 如何将Facebook / Twitter详细信息连接到当前经过身份验证的用户

时间:2016-05-01 11:07:29

标签: php laravel laravel-5.1 laravel-socialite

我已经很好地实施了Socialite,适用于与我的应用数据库中的电子邮件地址具有相同Facebook电子邮件地址的新用户或用户。

但是,某些用户将拥有与他们在我的应用中使用的帐户不同的Facebook电子邮件帐户。我想允许用户在登录时将他们的Facebook个人资料连接到他们的应用帐户。

以下是我的路线:

Route::get('login/{provider}', 'Auth\AuthController@redirectToProvider');
Route::get('login/{provider}/callback', 'Auth\AuthController@handleProviderCallback');

redirectToProvider代码:

public function redirectToProvider($provider)
{
    return Socialite::driver($provider)->redirect();
}

以下是处理社交名媛用户创建/更新的当前代码:

public function handleProviderCallback($provider)
    {
        $user = Socialite::driver($provider)->user();

        $user_query = User::where('email', $user->email)
            ->orWhere('oauth_facebook_id', $user->id)
            ->orWhere('oauth_twitter_id', $user->id)
            ->get();

        if($user_query->count() > 0) {

            $the_user = $user_query->first();

            if($provider === 'facebook') {
                $the_user->oauth_facebook_id = $user->id;
            }

            if($provider === 'twitter') {
                $the_user->oauth_twitter_id = $user->id;
            }

            $the_user->avatar = $user->avatar;

            $the_user->save();

            \Auth::login($the_user, true);

            return redirect('/dashboard');
        }

        $new_user = User::create([
            'name' => $user->name,
            'email' => $user->email,
            'oauth_facebook_id' => $provider === 'facebook' ? $user->id : NULL,
            'oauth_twitter_id' => $provider === 'twitter' ? $user->id : NULL,
            'avatar' => $user->avatar,
            'confirmed' => true,
            'gender' => $user->user['gender'],
        ]);

        \Auth::login($new_user, true);

        flash()->success('Success!', 'Your account has been created using your '.ucfirst($provider).' details!');
        return redirect('/dashboard');
    }

将返回的详细信息与当前经过身份验证的用户链接的最佳方式是什么?

提前致谢:)

1 个答案:

答案 0 :(得分:3)

只需检查用户是否已登录,然后使用提供商(oauth_{provider}_id)返回的数据更新该用户的$userData->id

否则,使用返回的数据创建一个新用户。

这样的事情:

public function handleProviderCallback($provider)
{
    $userData = Socialite::driver($provider)->user();

    if(Auth::check()) {

        $user = User::find(Auth::user()->id);

        if($provider === 'facebook') {
            $user->oauth_facebook_id = $userData->id;
        }

        if($provider === 'twitter') {
            $user->oauth_twitter_id = $userData->id;
        }

        $user->save();

    } else {

        $new_user = User::create([
            'name' => $userData->name,
            'email' => $userData->email,
            'oauth_facebook_id' => $provider === 'facebook' ? $userData->id : NULL,
            'oauth_twitter_id' => $provider === 'twitter' ? $userData->id : NULL,
            'avatar' => $userData->avatar,
            'confirmed' => true,
            'gender' => $userData->user['gender'],
        ]);

        \Auth::login($new_user, true);

        flash()->success('Success!', 'Your account has been created using your '.ucfirst($provider).' details!');

    }

    return redirect('/dashboard');
}