我使用Google和Facebook进行了社交名流登录,但是在SocialiteController部分中出现了类似上述问题的错误。
这是我的SocialiteController
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Role;
use App\SocialAccount;
use App\User;
class SocialiteController extends Controller
{
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
public function handleProviderCallback($provider)
{
$user = Socialite::driver($provider)->user();
$authUser = $this->findOrCreateUser($user, $provider);
Auth::login($authUser, true);
return redirect('/personal');
}
public function findOrCreateUser($socialUser, $provider)
{
$socialAccount = SocialAccount::where('provider_id', $socialUser->getId())
->where('provider_name', $provider)
->first();
if($socialAccount) {
return $socialAccount->user;
} else {
$user = User::where('email', $socialUser->getEmail())->first();
if(!$user) {
$user = User::create([
'username' => $socialUser->getName(),
'email' => $socialUser->getEmail()
]);
$user->assignRole('Registered');
}
$user->socialAccounts()->create([
'provider_id' => $socialUser->getId(),
'provider_name' => $provider
]);
return $user;
}
}
}
这是我的用户模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use App\Profile;
use App\Article;
use App\Video;
use App\Images;
use App\News;
class User extends Authenticatable Implements MustVerifyEmail
{
use Notifiable, HasRoles;
protected $table = "users";
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'email', 'password'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function profile(){
return $this->hasOne(Profile::class);
}
public function article()
{
return $this->hasMany(Article::class);
}
public function socialAccounts()
{
return $this->hasOne(SocialAccount::class);
}
public function video(){
return $this->hasMany(Video::class);
}
public function news(){
return $this->hasMany(News::class);
}
}
完整的错误消息如下: 传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须实现Illuminate \ Contracts \ Auth \ Authenticatable接口,给定为null,在/ home / asyj6686 / public_html / sublaravel / vendor / laravel / framework / src / Illuminate / Auth中调用/AuthManager.php,第297行
答案 0 :(得分:0)
传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须实现Illuminate \ Contracts \ Auth \ Authenticatable接口,给定null
此错误非常简单。这意味着您已经向登录名传递了null
值。
Auth::login($authUser, true);
我看不到所提供的代码有什么问题。因此,我猜测您可能只是忘记了在 SocialAccount 模型中添加与 User 的逆关系。这将导致$socialAccount->user
返回null
并生成您收到的错误。
App \ SocialAccount.php
class SocialAccount extends Model
{
// ...
public function user()
{
return $this->belongsTo(User::class);
}
}
请注意,用户应该不能->hasMany()
SocialAccount 吗?