1

我已经很好地实现了社交名流,并且适用于新用户或与我的应用程序数据库中的电子邮件地址具有相同 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');
    }

将返回的详细信息与当前经过身份验证的用户联系起来的最佳方法是什么?

提前致谢 :)

4

1 回答 1

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');
}
于 2016-05-01T11:17:23.943 回答