0

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

Jhonny Montoya
  • 113
  • 1
  • 2
  • 11

2 Answers2

4

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

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

you should use try catch method for deny catch


try {

    if (!$request->has('code') || $request->has('denied')) {
       return redirect('/');       // redirect url
    }

    $user = Socialite::driver('facebook')->user();
}
catch (\InvalidArgumentException $e) {
    return redirect()->to('/');   // redirect url
}
Nishal K.R
  • 1,090
  • 11
  • 21