问题
当我尝试使用Facebook登录时(使用Laravel Socialite),它返回错误的回调路由,并抛出NotFoundHttpException。
/回调?代码= xxxxxxxxxxxxxxxxx
我认为它应该返回callback / facebook?code = xxxxxx以使登录工作。
Route.php
Route::get('/callback/{provider}', 'SocialAuthController@callback');
Route::get('/redirect/{provider}', 'SocialAuthController@redirect');
Service.php
'facebook' => [
'client_id' => env('FB_CLIENT_ID_DEV'),
'client_secret' => env('FB_CLIENT_SECRET_DEV'),
'redirect' => env('FB_REDIRECT_DEV'),
],
.ENV
FB_CLIENT_ID_DEV = 'xxxxxxxxxxx'
FB_CLIENT_SECRET_DEV = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
FB_REDIRECT_DEV = 'http://localhost:8000/callback/facebook'
SocialAuthController
public function redirect($provider)
{
return Socialite::driver($provider)->redirect();
}
public function callback(SocialAccountService $service, $provider)
{
// Important change from previous post is that I'm now passing
// whole driver, not only the user. So no more ->user() part
$user = $service->createOrGetUser(Socialite::driver($provider));
auth()->login($user);
return redirect()->to('index');
}
SocialAccountService
class SocialAccountService
{
public function createOrGetUser(Provider $provider)
{
$providerUser = $provider->user();
$providerName = class_basename($provider);
$account = SocialAccount::whereProvider($providerName)
->whereProviderUserId($providerUser->getId())
->first();
if ($account) {
return $account->user;
} else {
$account = new SocialAccount([
'provider_user_id' => $providerUser->getId(),
'provider' => $providerName
]);
$user = User::whereEmail($providerUser->getEmail())->first();
if (!$user) {
$user = User::create([
'email' => $providerUser->getEmail(),
'name' => $providerUser->getName(),
'status' => 1,
'description' => "Saknas."
]);
}
$account->user()->associate($user);
$account->save();
return $user;
}
}
}
注意
我正在关注本教程:https://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-twitter-login.html#.WAZGQ-CLSUn。 使用Google登录的社交网站有效,但不适用于Facebook。