0

I'm trying to show a succes message when a person logs in with facebook (Socialite). But my controller is not redirecting with a session object for some reason. My facebook login is working properly. I think Laravel is using the protected $redirectPath = '/'; for this. How can I send a session with this path?

How I tried to achieve this :

AuthController :

protected $redirectPath = '/';

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

public function handleProviderCallback()
{
     try {
         $user = Socialite::driver('facebook')->user();
     } catch (Exception $e) {
         return redirect('auth/facebook');
     }

     $authUser = $this->findOrCreateUser($user);

     Auth::login($authUser, true);

     // I think this line is not executing annymore bevause of the Auth::login redirect
     return redirect()->back()->with('successfullFacebookLogin', Auth::user()->name);
}

In my view :

@if(session()->has('successfullFacebookLogin'))
<!-- Open login modal when successfully logged in with facebook -->

     @include('snippets.facebook-login-modal')

@endif

@if(session()->has('successfullFacebookLogin'))
    <!-- Open successfull login modal-->
    <script>
        $(document).ready(function() {
            $('#successfullFacebookLoginModal').modal('show');
        });
    </script>
@endif

My routes.php file (without irrelevant code) :

Route::group(['middleware' => ['web']], function () {
    // Logged in
    Route::group(['middleware' => 'auth'], function () {
        // Delete a user
        Route::get('/admin/user/delete/{id}',  'ProfileController@deleteUser');
    });

        // Facebook login
        Route::get('auth/facebook', array('as' => 'facebookLogin', 'uses' => "Auth\AuthController@redirectToProvider"));
    Route::get('auth/facebook/callback', 'Auth\AuthController@handleProviderCallback');
    // Get Register page
    Route::get('/register', array('as' => 'getRegister', 'uses' => 'Auth\AuthController@getRegister'));
    // Register
    Route::post('/register', array('as' => 'register', 'uses' => 'Auth\AuthController@register'));
    // Login
    Route::get('/login', array('as' => 'getLogin', 'uses' => 'Auth\AuthController@getRegister'));
    // Login
    Route::post('/login', array('as' => 'login', 'uses' => 'Auth\AuthController@login'));
    // Login (with modal)
    Route::post('/login/modal', array('as' => 'loginModal', 'uses' => 'Auth\AuthController@loginModal'));
    // Logout
    Route::get('auth/logout', array('as' => 'getLogout','uses' => 'Auth\AuthController@getLogout'));

});
Jim Peeters
  • 2,573
  • 9
  • 31
  • 53

1 Answers1

0

You are doing it wrong. You can't send data using with when you are redirecting as with is only used to pass data as variable to a view template.

In your case, you will need to use session to send the data.

Example:

<?php
session()->flash('successfullFacebookLogin', Auth::user()->name); 

And put it before your redirection code.

For more about session: https://laravel.com/docs/5.2/session

  • I did it like this : Auth::login($authUser, true); session()->flash('successfullFacebookLogin', Auth::user()->name); return redirect()->back(); But it still redirects to the homepage without my message being shown – Jim Peeters Jun 19 '16 at 10:05
  • @JimPeeters can you `var_dump(session())` on your homepage after the redirection? what does it show? – Muhammad Sumon Molla Selim Jun 19 '16 at 10:10
  • it was a very big dump so here is a link with the dump : https://jsfiddle.net/9csb335L/ – Jim Peeters Jun 19 '16 at 10:48