我尝试在我的 Laravel 5.1 项目中实现https://github.com/laravel/socialite 。
因此,当我阅读文档时,我会写:在 services.php
'facebook' => [
'client_id' => env('FACEBOOK_APP_ID'),
'client_secret' => env('FACEBOOK_APP_SECRET'),
'redirect' => env('FACEBOOK_REDIRECT'),
],
我也在 app.php 中添加:
Laravel\Socialite\SocialiteServiceProvider::class,
和别名:
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
现在在 AuthController 我添加:
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
public function handleProviderCallback($provider)
{
//notice we are not doing any validation, you should do it
$user = Socialite::driver($provider)->user();
// stroing data to our use table and logging them in
$data = [
'name' => $user->getName(),
'email' => $user->getEmail()
];
Auth::login(User::firstOrCreate($data));
//after login redirecting to home page
return redirect($this->redirectPath());
}
和 route.php:
Route::get('/social/login/redirect/{provider}', ['uses' => 'Auth\AuthController@redirectToProvider', 'as' => 'social.login']);
Route::get('/social/login/{provider}', 'Auth\AuthController@handleProviderCallback');
但是现在,当我尝试访问http://www.domain.com/social/login/facebook时,我得到:
怎么了?我在哪里出错?如何解决这个问题?
更新: 我的 .env 文件是:
FACEBOOK_APP_ID=<app_id>
FACEBOOK_APP_SECRET=<app_secret>
FACEBOOK_REDIRECT=http://www.domain.com/social/login/redirect/facebook