0

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

2 Answers 2

6

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.

3
  • 1
    Thank you man! Saved me a lot of time. :) Commented Aug 30, 2023 at 11:54
  • Thank you. I've struggled with this so many hours. Facebook must give more information at least dev app.
    – gblue1223
    Commented Oct 20, 2023 at 10:52
  • Saved a lot of time. Thanks! Commented May 2 at 4:37
0

Go to https://developers.facebook.com/apps/your-app-id/use_cases/

then click edit for Authentication and account creation

next scroll down to Permissions

finally click add to email

Not the answer you're looking for? Browse other questions tagged or ask your own question.