jer*_*auf 2 facebook laravel laravel-socialite
我正在完全按照文档进行操作。
https://github.com/laravel/socialite和https://laravel.com/docs/5.1/authentication#social-authentication
我已经在 Facebook 上创建了我的应用程序并且一切正常。当我使用 Facebook 按钮单击登录时,它会授权该应用程序并将我带回我的网站。但是,它不会显示我已登录。如果我使用 dd() 而不是下面的重定向,我会从我的 Facebook 帐户中获取所有数据。但是只有登录用户才能看到的页面是不可见的。
这是我的控制器:
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback()
{
$user = Socialite::driver('facebook')->user();
return redirect('my-profile')
->with('message', 'You have signed in with Facebook.');
}
Run Code Online (Sandbox Code Playgroud)
以下是我的路线:
Route::get('login/facebook', 'Auth\AuthController@redirectToProvider');
Route::get('login/facebook/callback', 'Auth\AuthController@handleProviderCallback');
Run Code Online (Sandbox Code Playgroud)
Socialite 已正确安装在 composer.json 中。这些类在 config/app.php 中,我的 FB 应用程序的 ID 在 config/services.php 中。
关于为什么它不起作用的任何想法?
在该handleProviderCallback方法中,您需要创建并验证驱动程序返回的用户。
如果不存在则创建用户:
$userModel = User::firstOrNew(['email' => $user->getEmail()]);
if (!$userModel->id) {
$userModel->fill([.....]);
$userModel->save();
}
Run Code Online (Sandbox Code Playgroud)
然后验证用户:
Auth::login($userModel);
Run Code Online (Sandbox Code Playgroud)
您的方法将如下所示:
public function handleProviderCallback() {
$user = Socialite::driver('facebook')->user();
$userModel = User::firstOrNew(['email' => $user->getEmail()]);
if (!$userModel->id) {
$userModel->fill([.....]);//Fill the user model with your data
$userModel->save();
}
Auth::login($userModel);
return redirect('my-profile')
->with('message', 'You have signed in with Facebook.');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2238 次 |
| 最近记录: |