我已经很好地实现了社交名流,并且适用于新用户或与我的应用程序数据库中的电子邮件地址具有相同 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');
}
将返回的详细信息与当前经过身份验证的用户联系起来的最佳方法是什么?
提前致谢 :)