php - Laravel 5.3 从第二次开始记录一个新的 Facebook 用户

标签 php laravel-5 laravel-5.3

我正在使用 socialite 将登录与 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);

关于php - Laravel 5.3 从第二次开始记录一个新的 Facebook 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41527359/

相关文章:

php - Laravel: Blade foreach 循环引导列

php - 在没有where子句的情况下计算yii2中表中的所有记录

php - Symfony:如何从文件加载自定义捆绑配置?

php - 找不到 Laravel 5 类 'Illuminate\Support\Facades\FormFacade'

php - 更新单个查询中匹配条件的最后一条记录

php - Eloquent:find() 和 where() 用法 laravel

javascript - 在javascript中显示php数组

php - MYSQL 5.6.12 中mysql插入查询出错

php - 为什么我的创建表单请求在 Laravel5 中抛出 404 异常?

php - 为什么我在尝试使用自定义用户提供程序登录时收到此 "Class App\Listeners\LogAuthenticationAttempt does not exist"错误?