amchart amchart - 3 months ago 18
PHP Question

laravel facebook no redirect back if app allowed already

I made the auth with socialite/facebook.

My code --

config/services.php:

'facebook' => [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'http://www.example.com/facebook/callback',
],


routes.php:

Route::get('facebook', 'FacebookController@redirectToProvider');
Route::get('facebook/callback', 'FacebookController@handleProviderCallback');


FacebookController.php:

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

public function handleProviderCallback(Request $request, User $user)
{
$users = Socialite::with('facebook')->user();

// user registration and login
// if the user is in the database, just login

return redirect()->back();
}


Everything works fine, the registration, the login and the redirect until one point.

If the app is already allowed, it redirects back normally. If you first authenticate with Facebook and allow the app, it does not redirect.

However, if the redirect is the homepage:

return redirect('/');


then it works too.

Answer

So i found the solution here: http://laravel.io/forum/08-03-2014-redirect-to-the-second-last-page

My solved code:

public function redirectToProvider()
{
    // added this
    Session::flash('url',$_SERVER['HTTP_REFERER']);

    return Socialite::with('facebook')->redirect();
}

public function handleProviderCallback(Request $request, User $user)
{
    $users = Socialite::with('facebook')->user();

    // user registration and login
    // if the user is in the database, just login

    // redirect changed from redirect()->back(); to this
    return Redirect::to(Session::get('url'));
}