dougu1045 2016-05-30 15:34
浏览 41
已采纳

为什么我不能使用laravel使用oauth登录存储facebook id和avatar路径?

I am using Socialite to login/register with facebook into my application. When I dump my $facebookUser variable I see this json :

$facebookuser :

enter image description here

But when I try to store the id and avatar , it doesn't store it and I can't display the users profile picture of facebook. I am using laravel to store my user.

AuthController.php :

public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('facebook')->user();
    } catch (Exception $e) {
        return redirect('auth/facebook');
    }

    $authUser = $this->findOrCreateUser($user);

    Auth::login($authUser, true);

    return redirect()->route('home')->with('successfullFacebookLogin', Auth::user()->name);
}

private function findOrCreateUser($facebookUser)
{
    // When I dd($facebookuser) it gives json stated above
    $authUser = User::where('facebook_id', $facebookUser->id)->first();

    if ($authUser){
        return $authUser;
    }

    return User::create([
        'name' => $facebookUser->name,
        'email' => $facebookUser->email,
        'facebook_id' => $facebookUser->user['id'],
        'avatar' => $facebookUser->avatar,
        'facebookAccount' => 1

    ]);
 }
  • 写回答

1条回答 默认 最新

  • duankuai6991 2016-05-30 15:41
    关注

    Use Laravel Socialite provided methods to access user details rather than access the property directly, here is list of available methods for all built-in providers:

    $user->getId();
    $user->getNickname();
    $user->getName();
    $user->getEmail();
    $user->getAvatar();
    

    So your code should be:

    private function findOrCreateUser($facebookUser)
    {
        // When I dd($facebookuser) it gives json stated above
        $authUser = User::where('facebook_id', $facebookUser->id)->first();
    
        if ($authUser){
            return $authUser;
        }
    
        return User::create([
            'name' => $facebookUser->getName(),
            'email' => $facebookUser->getEmail(),
            'facebook_id' => $facebookUser->getId(),
            'avatar' => $facebookUser->getAvatar(),
            'facebookAccount' => 1
    
        ]);
     }
    

    Don't forget to state those columns above in $fillable property of User model:

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'facebook_id', 'avatar', 'facebookAccount'
    ];
    

    Otherwise fill the attributes manually:

    $user = new User;
    $user->name = $facebookUser->getName();
    $user->email = $facebookUser-> getEmail();
    $user->facebook_id = $facebookUser->getId();
    $user->facebookAccount = 1;
    $user->save();
    
    return $user;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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,如何解決?