“Error ‘Sorry, something went wrong.’ while logging in Facebook Laravel”

huangapple go评论79阅读模式
英文:

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&amp;redirect_uri=https%3A%2F%2Fgrowexspeak.space%2Ffacebook%2Fcallback&amp;scope=email&amp;response_type=code

I leave the authorization code through facebook made through on Laravel.

FacebookController.php

&lt;?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(&#39;facebook&#39;)-&gt;stateless()-&gt;redirect();

       return $facebook_user;
    }
     public function callbackFromFacebook()
     {
      try {
           $user = Socialite::driver(&#39;facebook&#39;)-&gt;stateless()-&gt;user();

           $saveUser = User::updateOrCreate([
               &#39;facebook_id&#39; =&gt; $user-&gt;getId(),
           ],[
               &#39;name&#39; =&gt; $user-&gt;getName(),
               &#39;email&#39; =&gt; $user-&gt;getEmail(),
               &#39;password&#39; =&gt; Hash::make($user-&gt;getName().&#39;@&#39;.$user-&gt;getId())
                ]);

           Auth::loginUsingId($saveUser-&gt;id);

           return redirect()-&gt;route(&#39;home&#39;);
           } catch (\Throwable $th) {
              throw $th;
           }
       }

}

config/services.php

facebook&#39; =&gt; [
        &#39;client_id&#39; =&gt; &#39;####&#39;, //USE FROM FACEBOOK DEVELOPER ACCOUNT
        &#39;client_secret&#39; =&gt; &#39;####&#39;, //USE FROM FACEBOOK DEVELOPER ACCOUNT
        &#39;redirect&#39; =&gt; &#39;https://growexspeak.space/facebook/callback&#39;
    ],

Web.php


Route::prefix(&#39;facebook&#39;)-&gt;name(&#39;facebook.&#39;)-&gt;group( function(){
    Route::get(&#39;auth&#39;, [FaceBookController::class, &#39;loginUsingFacebook&#39;])-&gt;name(&#39;login&#39;);
    Route::get(&#39;callback&#39;, [FaceBookController::class, &#39;callbackFromFacebook&#39;])-&gt;name(&#39;callback&#39;);
});

App.php

&#39;providers&#39; =&gt; [
        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 &quot;lazy&quot; loaded so they don&#39;t hinder performance.
    |
    */

    &#39;aliases&#39; =&gt; Facade::defaultAliases()-&gt;merge([
        // &#39;ExampleClass&#39; =&gt; App\Example\ExampleClass::class,
        &#39;Socialite&#39; =&gt; Laravel\Socialite\Facades\Socialite::class,

    ])-&gt;toArray(),

auth.blade.php


 &lt;a href=&quot;{{ route(&#39;facebook.login&#39;) }}&quot; class=&quot;btn btn-facebook btn-user btn-block&quot;&gt;
   &lt;i class=&quot;fab fa-facebook-f fa-fw&quot;&gt;&lt;/i&gt;
   Login with Facebook
&lt;/a&gt;

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

Screenshot: Use cases

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.

huangapple
  • 本文由 发表于 2023年6月29日 20:31:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定