douzhushen_9776 2017-01-25 00:12
浏览 48
已采纳

带有Socialite的Laravel 5.3中的SessionGuard.php中的ErrorException(Facebook作为提供者)

I got a Laravel project (v5.3) which needs a login with a custom form and with facebook connect.

So, I use Socialite and it's what I need, however, when I login for first time, the callback doesn't redirect to the view/url that I indicate, instead Laravel shows me the next Exception:

ErrorException in SessionGuard.php line 439: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /var/www/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 294 and defined

This is the code in my custom controller :

public function handleProviderCallback()
{
    try
    {
        $socialUser = Socialite::driver('facebook')->user();

    }catch(\Exception $e)
    {
        return redirect('/');
    }
        $user = User::where('facebook_id', $socialUser->getId())->first();

        if(!$user)
            User::create([
                'facebook_id' => $socialUser->getId(),
                'name' => $socialUser->getName(),
                'email' => $socialUser->getEmail(),
            ]);

            auth()->login($user);
            return redirect()->to('/home');              
}

So, I check my database and the record is there - Ok ...but I'm not in session - wrong ... if I login with facebook again (keeping the record in the database) I login successfully and the error is gone... Ok but at all...

If I do:

dd($user)

I got a null value the first time In the following times I got nothing...the $user var show in blank...

So I think is this line:

auth()->login($user);

And I tried

\Auth::login($user);

but it doesn't work too...

So, what's could be wrong, any configuration in my project? This example I saw it in tutoriales with Laravel 5.3 so I don't understand what's going on :(

Thank you! And sorry for my english! :)

  • 写回答

1条回答 默认 最新

  • douchuza8908 2017-01-25 00:37
    关注

    Do this:

    if(!$user)
            $user = User::create([
                'facebook_id' => $socialUser->getId(),
                'name' => $socialUser->getName(),
                'email' => $socialUser->getEmail(),
            ]);
    

    I added $user = to your existing code. Because when you do Model::create it returns an object of type Model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?