好的,所以我阅读了如何在我的应用程序中实现 Laravel 社交名流,这样我就可以让用户使用 Google 或 Facebook 登录。我在这里阅读了如何做。我在使用这种方法时遇到的问题是,如果用户使用电子邮件 myemail@example.com 从 Google 登录,然后使用 myemail@example.com 从 Facebook 登录,他们将登录到同一个帐户!安全问题对。所以我认为在让他们登录之前,我会检查您可以获得的提供者 ID,但是当我尝试比较他们创建帐户时存储在数据库中的 provider_id 与存储在社交名流用户变量中的 provider_id 时,我得到这个错误:
传递给 Illuminate\Auth\SessionGuard::login() 的参数 1 必须实现接口 Illuminate\Contracts\Auth\Authenticatable,给出 Illuminate\Http\RedirectResponse 的实例
这是我用于社交名流的所有代码:
<?php
namespace App\Http\Controllers;
use Socialite;
use App\User;
use Auth;
use Illuminate\Support\Facades\Redirect;
use Flash;
use Illuminate\Http\Request;
use App\Http\Requests;
class SocialiteController extends Controller
{
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
public function handleProviderCallback($provider)
{
try
{
$social_user = Socialite::driver($provider)->user();
}
catch(Exception $e)
{
return Redirect::to('auth/' . $provider);
}
$authUser = $this->findOrCreateUser($social_user);
Auth::login($authUser, true);
flash()->overlay('You have been logged in successfully!', 'Congratulations');
return Redirect::to('/');
}
//create a new user in our database or grab existing user
private function findOrCreateUser($social_user)
{
if ($authUser = User::where('email', $social_user->email)->first()) {
//this creates an error
if($authUser->provider_id == $social_user->id)
return $authUser;
else
{
flash()->overlay('An account for that email already exists!', 'Error');
return Redirect::to('/');
}
}
return User::Create([
'provider_id' => $social_user->id,
'name' => $social_user->name,
'email' => $social_user->email,
'nickname' => $social_user->nickname,
'avatar_img' => $social_user->avatar,
'role_id' => 1, //set role to guest
'social_login' => true //tell the database they are logging in from oauth
]);
}
}