英文:
Error "Sorry, something went wrong." while logging in facebook laravel
问题
I have implemented authorization through Facebook using Laravel. When I am on the site and click on the authorization button via Facebook, there is a redirect to a page with the error message "Sorry, something went wrong. We're working on getting this fixed as soon as we can."
URL where the error is located: https://www.facebook.com/v3.3/dialog/oauth?client_id=2040703196275670&redirect_uri=https%3A%2F%2Fgrowexspeak.space%2Ffacebook%2Fcallback&scope=email&response_type=code
Here is the code for Facebook authorization in Laravel:
FacebookController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Socialite\Facades\Socialite;
class FaceBookController extends Controller
{
public function loginUsingFacebook()
{
$facebook_user = Socialite::driver('facebook')->stateless()->redirect();
return $facebook_user;
}
public function callbackFromFacebook()
{
try {
$user = Socialite::driver('facebook')->stateless()->user();
$saveUser = User::updateOrCreate([
'facebook_id' => $user->getId(),
],[
'name' => $user->getName(),
'email' => $user->getEmail(),
'password' => Hash::make($user->getName().'@'.$user->getId())
]);
Auth::loginUsingId($saveUser->id);
return redirect()->route('home');
} catch (\Throwable $th) {
throw $th;
}
}
}
config/services.php
'facebook' => [
'client_id' => '####', // Use your Facebook Developer Account credentials
'client_secret' => '####', // Use your Facebook Developer Account credentials
'redirect' => 'https://growexspeak.space/facebook/callback'
],
Web.php
Route::prefix('facebook')->name('facebook.')->group( function(){
Route::get('auth', [FaceBookController::class, 'loginUsingFacebook'])->name('login');
Route::get('callback', [FaceBookController::class, 'callbackFromFacebook'])->name('callback');
});
App.php
'providers' => [
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => Facade::defaultAliases()->merge([
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
])->toArray(),
auth.blade.php
<a href="{{ route('facebook.login') }}" class="btn btn-facebook btn-user btn-block">
<i class="fab fa-facebook-f fa-fw"></i>
Login with Facebook
</a>
I hope this helps you in finding a solution to your issue.
英文:
I have implemented authorization through facebook using laravel.
When I am on the site, and I click on the authorization button via facebook, then there is a redirect to the page with the error "Sorry, something went wrong. We're working on getting this fixed as soon as we can".
enter image description here
Already spent almost all, and did not find a solution for this problem.
URL where the error is located https://www.facebook.com/v3.3/dialog/oauth?client_id=2040703196275670&redirect_uri=https%3A%2F%2Fgrowexspeak.space%2Ffacebook%2Fcallback&scope=email&response_type=code
I leave the authorization code through facebook made through on Laravel.
FacebookController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Socialite\Facades\Socialite;
class FaceBookController extends Controller
{
public function loginUsingFacebook()
{
$facebook_user = Socialite::driver('facebook')->stateless()->redirect();
return $facebook_user;
}
public function callbackFromFacebook()
{
try {
$user = Socialite::driver('facebook')->stateless()->user();
$saveUser = User::updateOrCreate([
'facebook_id' => $user->getId(),
],[
'name' => $user->getName(),
'email' => $user->getEmail(),
'password' => Hash::make($user->getName().'@'.$user->getId())
]);
Auth::loginUsingId($saveUser->id);
return redirect()->route('home');
} catch (\Throwable $th) {
throw $th;
}
}
}
config/services.php
facebook' => [
'client_id' => '####', //USE FROM FACEBOOK DEVELOPER ACCOUNT
'client_secret' => '####', //USE FROM FACEBOOK DEVELOPER ACCOUNT
'redirect' => 'https://growexspeak.space/facebook/callback'
],
Web.php
Route::prefix('facebook')->name('facebook.')->group( function(){
Route::get('auth', [FaceBookController::class, 'loginUsingFacebook'])->name('login');
Route::get('callback', [FaceBookController::class, 'callbackFromFacebook'])->name('callback');
});
App.php
'providers' => [
Laravel\Socialite\SocialiteServiceProvider::class,
],
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/
'aliases' => Facade::defaultAliases()->merge([
// 'ExampleClass' => App\Example\ExampleClass::class,
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
])->toArray(),
auth.blade.php
<a href="{{ route('facebook.login') }}" class="btn btn-facebook btn-user btn-block">
<i class="fab fa-facebook-f fa-fw"></i>
Login with Facebook
</a>
Until I know the solution
答案1
得分: 3
我昨晚花了很多时间尝试解决这个问题,问题出在Facebook应用程序设置中,因为我没有启用“电子邮件”使用情况权限。
要检查这个,请转到 https://developers.facebook.com/apps/your-app-id/use_cases/ 并点击"身份验证和帐户创建" > "编辑"按钮。
如果在“电子邮件”权限旁边有一个“添加”按钮,请点击它,这应该可以让Facebook Socialite Driver正常工作。
希望对某人有所帮助。
英文:
I spent many hours last night trying to figure this out and the issue was in the Facebook App settings, as I did not have the "email" use case permission enabled.
To check this, goto https://developers.facebook.com/apps/your-app-id/use_cases/ and click the Authentication and account creation > Edit button
If there is an Add button next to the "email" permission, click it and that should allow the Facebook Socialite Driver to work.
Screenshot: Add email permission
Hope this helps someone.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论