我使用laravel socilialite创建facebook登录信息。
这是我的提供商重定向
public function redirectToProvider($provider = null)
{
if (!config("services.$provider"))
abort('404'); //just to handle providers that doesn't exist
if($provider == "facebook"){
return Socialite::driver('facebook')
->fields(['first_name', 'last_name', 'email', 'gender', 'birthday'])
->scopes(['email', 'user_birthday'])->redirect();
}
}
然而,在我的回调中,我转储我的$用户以查看我收到的数据
public function findOrCreateUser($user, $provider)
{
$authUser = User::where('provider_id', $user->id)->first();
if ($authUser) {
return $authUser;
}
switch ($provider) {
case "facebook":
var_dump($user);
return User::create([
'firstname' => $user->user['first_name'],
'lastname' => $user->user['last_name'],
'email' => $user->user['email'],
'email_verified' => true,
'provider' => $provider,
'provider_id' => $user->user['id']
]);
break;
case "google":
return User::create([
'firstname' => $user['name']['givenName'],
'lastname' => $user['name']['familyName'],
'email' => $user['email'],
'email_verified' => true,
'provider' => $provider,
'provider_id' => $user['id']
]);
break;
}
}
这是我从facebook
收到的数据object(Laravel\Socialite\Two\User)[215]
public 'token' => string '******' (length=178)
public 'refreshToken' => *****
public 'expiresIn' => int ******
public 'id' => string '*****' (length=15)
public 'nickname' => null
public 'name' => string '******' (length=15)
public 'email' => string '*****' (length=24)
public 'avatar' => string '******' (length=67)
public 'user' =>
array (size=6)
'name' => string '*****' (length=15)
'email' => string '*****' (length=24)
'gender' => string '****' (length=4)
'verified' => boolean ****
'link' => string '*****' (length=60)
'id' => string '****' (length=15)
public 'avatar_original' => string '****' (length=66)
public 'profileUrl' => string '*****' (length=60)
我可以尽可能地没有收到我要求的所有数据,但我无法找到原因。
如何确保Facebook向我提供用户的first_name
和last_name
?
答案 0 :(得分:2)
这是我的工作代码:
try
{
$socialUser = Socialite::driver('facebook')->fields(['name', 'first_name', 'last_name', 'email'])->user();
}
catch (Exception $e)
{
\Log::error($e);
return redirect()->route('register')->with('error', 'ERROR');
}
$user = new \StdClass;
$user->id = $socialUser->id;
$user->name = $socialUser->name;
$user->email = $socialUser->email;
$user->firstname = $socialUser->user['first_name'];
$user->lastname = $socialUser->user['last_name'];
尝试删除->scopes(['email', 'user_birthday'])
或将其替换为我的代码。
编辑:我认为我有同样的问题而->user()
是解决方案!