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

带有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! :)

我有一个Laravel项目(v5.3)需要使用自定义表单和facebook连接登录。

所以,我使用Socialite,这就是我需要的东西,但是,当我第一次登录时,回调不会重定向到我指示的视图/网址,而是Laravel向我显示 下一个异常: p>

SessionGuard.php第439行中的ErrorException: 传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须实现接口Illuminate \ Contracts \ Auth \ Authenticatable,null给定,在第294行的/var/www/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php中调用并定义 p> blockquote>

这是我的自定义控制器中的代码: p>

  public function handleProviderCallback()
 {
 try 
 {
 $ socialUser = Socialite :: driver('facebook  ') - > user(); 
 
} catch(\ Exception $ e)
 {
返回重定向('/'); 
} 
 $ user = User :: where('facebook_id'  ,$ socialUs  er-> getId()) - > first(); 
 
 if if(!$ user)
 User :: create([
'facebook_id'=>  $ socialUser-> getId(),
'name'=>  $ socialUser-> getName(),
'email'=>  $ socialUser-> getEmail(),
]); 
 
 
 
认证() - >登录($ user); 
返回redirect() - > to('/ home');  
} 
  code>  pre> 
 
 

所以,我检查了我的数据库,记录就在那里 - 好吧......但是我不在会话中 - 错误 。 ..如果我再次登录facebook(保留记录在数据库中)我成功登录并且错误消失了...好吧但是根本没有... p>

如果我这样做:

  dd($ user)
  code>  pre> 
 
 

我第一次得到 null em>值 在以下时间我没有得到任何东西...... $ user var显示为空白... p>

所以我认为是这一行: p>

   auth() - > login($ user); 
  code>  pre> 
 
 

我试过 p>

  \  Auth :: login($ user); 
  code>  pre> 
 
 

但它也不起作用... p>

那么,什么是 可能是错的,我项目中的任何配置? 这个例子我在Laravel 5.3的tutoriales中看到它,所以我不明白发生了什么:( p>

谢谢!对不起我的 英语! :) p> div>

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.