3
votes

I use laravel 5.6 and laravel/socialite: "^3.2.0" for auth by Facebook but while login to facebook user email is null (user facebook account has email)

my code for auth :

public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)->redirect();
    }

public function handleProviderCallback($provider)
    {
        $user = Socialite::driver($provider)->user();

        dd($user);
        $authUser = $this->findOrCreateUser($user, $provider);
        Auth::login($authUser, true);
        return redirect($this->redirectTo);
    }

public function findOrCreateUser($user, $provider)
    {
        $authUser = User::where('provider_id', $user->id)->first();
        if ($authUser) {
            return $authUser;
        }

        return User::create([
            'name'     => $user->name,
            'email'    => $user->email,
            'provider' => $provider,
            'provider_id' => $user->id
        ]);
    }


  [1]: https://i.stack.imgur.com/6zYEI.png
1

1 Answers

1
votes

There is no error in your code.


Facebook users register using a Mobile number instead of Email address. This means that Facebook doesn't have an email address for that specific user. So this will be giving you back NULL

Please check this under the section (Email)