Search code examples
laravel-5laravel-socialite

Laravel 5.2.* Laravel/socialite catch when facebook deny access


I'm using Laravel 5.2.* and laravel/socialite 2.0.14 and everything is fine when when trying login to facebook and in Facebook click "Okay".

But if I try to login with Facebook for the very first time and if I Click "Cancel" on Facebook to deny access, it redirect to the callback URI and triggers and error.

enter image description here

enter image description here

Here is my Controller code,

namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Socialite;

class SocialiteController extends Controller
{
    public function __construct()
    {
    }

    public function facebook()
    {
        return Socialite::driver('facebook')->redirect();
    }

     public function facebookCallback()
     {
        $user = Socialite::driver('facebook')->user();

        // $user->token;

        dd($user);
    }
}

Does anyone know how to catch the error or solve it?

Thanks


Solution

  • You can use try catch to catch exceptions. Like this:

    try {
        $user = Socialite::driver('facebook')->user();
    }
    catch (\Exception $e) {
        do something.....
    }