需要查看您的路线配置以获取更多信息,但会根据您提供的信息进行尝试。
请注意,您将被重定向到http://localhost:8000/auth/login#_=_
.
看起来您在 Facebook 上授权后被重定向到应用程序的登录页面.
In config/services.php
,确保对于您的 facebook 登录配置,您的redirect
url 指向您在路由配置中定义的用于处理回调的路由。
e.g.
// config/services.php
...
'facebook' => [
'client_id' => 'my_facebook_client_id',
'client_secret' => 'my_facebook_client_secret',
'redirect' => 'http://localhost:8000/auth/facebook/callback',
],
...
路线http://localhost:8000/auth/facebook/callback
然后应该在路由配置中定义:
// app/Http/routes.php
...
Route::get('auth/facebook/callback', 'Auth\AuthController@handleProviderCallback');
...
请注意,路由配置必须允许GET
请求,因为用户将被重定向。
Your dd($user)
使用此设置应该可以正常工作。
如何保存用户数据取决于您的模型架构。
例如,在一个简单的情况下,您没有其他注册方法并且仅使用 facebook 登录,您可以这样做:
public function handleProviderCallback()
{
$facebookData = Socialite::driver('facebook')->user();
// check if already in DB
try{
$user = User::where('facebook_id', $data->id)->firstOrFail();
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
// create a new user
$user = new User();
// set the properties you want
// $user->facebook_id = $data->id;
// ...
// then save
$user->save();
}
// login the user
Auth::login($user);
// perhaps return a redirect response
return redirect()->action('MyController@myAction');
}