Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.3第二次登录Facebook新用户_Php_Laravel 5_Laravel 5.3 - Fatal编程技术网

Php Laravel 5.3第二次登录Facebook新用户

Php Laravel 5.3第二次登录Facebook新用户,php,laravel-5,laravel-5.3,Php,Laravel 5,Laravel 5.3,我正在使用社交名将登录与Facebook功能集成。这是我的密码: public function handleProviderCallback() { try { $socialUser = Socialite::driver('facebook')->user(); } catch (\Exception $e) { return redirect('/dashboard'); } /*section1 : i che

我正在使用社交名将登录与Facebook功能集成。这是我的密码:

public function handleProviderCallback()
{

    try {
        $socialUser = Socialite::driver('facebook')->user();
    } catch (\Exception $e) {
        return redirect('/dashboard');
    }


    /*section1 : i check if the user exists in the db, and if no then 
    I save this user in the db */

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

        Auth::loginUsingId($socialUser->id);
        return redirect()->intended('/dashboard');
        }

        /* end of section one */


        else{
        if(Auth::loginUsingId($user->id)){
            return redirect()->intended('/dashboard');
        }
    }


}

问题是:我用一个新用户登录,数据被添加到数据库,我被重定向到仪表板,但我没有登录。再次尝试登录后(当用户已存在于数据库中时),我已成功登录。为什么?谢谢

问题在于以下几行。您必须在数据库中传递用户id,而不是Facebook id

Auth::loginUsingId($socialUser->id)

更新如下

$newUser = User::create(...);
Auth::loginUsingId($newUser->id);

我在RegisterController.php第104行的HandleExceptions->handleError('8','试图获取非对象的属性','/Users/andrei/Desktop/service/service.dev/app/Http/Controllers/Auth/RegisterController.php','104',array('socialUser'=>object(User),'User'=>null))处出错。(对于Auth::loginUsingId($user->id);)@AndrewT
create
函数将返回创建的实例。从错误中看,它似乎没有创建条目。希望您将
create
函数的返回值存储为
$user=user::create(…)
。是否可以检查是否在数据库中创建了条目?数据已成功添加到数据库中。问题是,我无法立即与该用户登录,因为我在更改上述代码后遇到错误,或者我被重定向到仪表板,但没有登录,我不明白为什么。我假设$user是在我退出if语句后才创建的,所以我不能在if语句中使用$user。好吧,我想你把
$user
$user=user::where('facebook\u id',$socialUser->getId())->first()混淆了。这不是我要说的。可以像下面那样更改变量并打印它<代码>$newUser=User::create(…);dd(新用户);Auth::loginUsingId($newUser->id)@AndrewT很高兴它帮助了你。我更新了答案中的变量名