'laravel 5.6 socialite auth by facebook email is null
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
Solution 1:[1]
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)
Solution 2:[2]
Yes, there is no error in your code. There are some additional things you need to enable to solve this problem.
you have to change the email permission from "standard access" to "advanced access".
To enable this permission, you need to enter the in the below URL
https://developers.facebook.com/apps/<app-id>/app-review/permissions/
after changing the permission, you will start receiving the email in the payload.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Udhav Sarvaiya |
Solution 2 | Sachin Saini |