In Laravel Socialite We are redirected to facebook. But When User Cancels (not allowing facebook to access public profile) it is giving error Missing Authorization exception
ClientException in RequestException.php line 107: Client error: GET https://graph.facebook.com/oauth/access_token?client_id=1309844325833234&client_secret=1304bbdd28400tret49a295d324d577c&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fauth%2Ffacebook%2Fcallback` resulted in a 400 Bad Request response: {"error":{"message":"Missing authorization code","type":"OAuthException","code":1,"fbtrace_id":"Aq9wMwG6ewl"}}
I dont want to display this Instead I Want to return to my site home page by giving a message "Facebook Login Failed" like what shown in stackoverflow facebook login.
Finally i got answer.Here it is
public function handleProviderCallback()
{
try {
$user = Socialite::driver('facebook')->user();
} catch (\Exception $e) {
//Here you can write excepion Handling Logic
}
}
Try catch didn't give good result to me. I used below methods to catch this error. If you're using Laravel Socialite library definitely it has function call handleProviderCallback
. In that use this code
handleProviderCallback
Method
/**
* Obtain the user information from GitHub.
*
* @return Response
*/
public function handleProviderCallback()
{
$error_code = Input::get('error_code');
if ($error_code == 200)
{
return redirect()->route('sign-in')->with('error','You\'ve chose not to grant us permission to connect with your Facebook account.');
}
else
{
$fbUser = Socialite::driver('facebook')->user();
# rest of your code
}
}
error_code
come from ??Well, If you look at the error page(Laravel Black screened) come to you check the URL of that page. It has the these get methods error
, error_code
, error_description
, error_reason
, state
.
Ex :
http://localhost:8000/login/facebook/callback?error=access_denied&error_code=200&error_description=Permissions+error&error_reason=user_denied&state=mIxNjoDCogT2piMV5LX1Imk6GWNzqPUt3JZaqsIo#_=_
You can use a switch
statement based on error Check this Facebook error codes, with an error message and pass it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With