簡體   English   中英

社交名流:傳遞給 Illuminate\Auth\SessionGuard::login() 的參數 1 必須實現接口 Illuminate\Contracts\Auth\Authenticatable,給定 null

[英]Socialite : Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

我使用 Google 和 Facebook 進行了社交名流登錄,但在 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;
        }
    }
}

這是我的用戶 model

<?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/ 中調用框架/src/Illuminate/Auth/AuthManager.php 上線 297

傳遞給 Illuminate\Auth\SessionGuard::login() 的參數 1 必須實現接口 Illuminate\Contracts\Auth\Authenticatable,給定 null

這個錯誤非常簡單。 這意味着您已將null值傳遞給登錄。

Auth::login($authUser, true);

我認為提供的代碼沒有任何問題。 因此,我猜測您可能只是忘記在SocialAccount model 中添加與User的反比關系。 這將導致$socialAccount->user返回null並生成您收到的錯誤。

應用\SocialAccount.php

class SocialAccount extends Model
{
    // ...

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

附帶說明一下,用戶不應該能夠->hasMany() SocialAccount嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM