doucou19961205 2019-03-06 10:34
浏览 247
已采纳

Laravel与facebook登录

I am having troubles in logging in with Facebook.

I am using Laravel Socialite, I have followed all the instructions but I am having problems at method callback.

This is what i do :

public function redirectToFacebook()
{
    return Socialite::driver('facebook')->redirect();
}
public function handleFacebookCallback()
{
    $userSocial = Socialite::driver('facebook')->user();
    //return $userSocial;
    $finduser = User::where('facebook_id', $userSocial->id)->first();
    if($finduser){
        Auth::login($finduser);
        return Redirect::to('/');
    }else{
        $new_user = User::create([
           'name'        => $userSocial->name,
           'email'       => $userSocial->email,
           'image'       => $userSocial->avatar_original,
           'facebook_id' => $userSocial->id
        ]);
        Auth::login($new_user);
        return redirect()->back();
    }
}

SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value

How to get the password of facebook?

  • 写回答

1条回答 默认 最新

  • doulvyi2172 2019-03-06 10:42
    关注

    When using Socialite you don't need the password of Facebook or any other service, because you will receive a token from the platform that you can use instead of a username / password.

    The problem here is that you are trying to create a user without supplying a password, while the password field is not nullable in the users table.

    You can solve this for example by creating a migration that changes the password column and makes it nullable, i.e.:

    Schema::table('users', function (Blueprint $table) {
        $table->string('password')->nullable()->change();
    });
    

    Still, you have to decide if this is the authorization setup you want and if you want users in your database without a password.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?