xuanyue
xuanyue

Reputation: 1428

Laravel $redirectPath doesn't work

I'm using the Laravel socialite to enable social login. After the user gets back from the social site, I'll redirect to a dashboard page in forms like /dashboard/id.

Now, when I directly close this dashboard page, and reopen the login page (url: auth/login) in the browser. It begins to redirect to the default /home. But the wired thing is, it doesn't go through the default getLogin() method so the $redirectPath I set in the AuthController doesn't work at all.

In the sociallite, I used the Auth::login() method to authenticated the user.

Laravel version is 5.1.

Can anyone explain what's the logic behind this? Why that getLogin method is not called?

Socialite callback:


public function handleProviderCallback()
    {
        $user = Socialite::with('facebook')->user();
        $authuser = $this->findOrCreateUser($user);
        Auth::login($authuser, true);
        //todo: here it should direct to lobby.
        return redirect()->intended('/lobby/'.$authuser->id);
    }

AuthController:


class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & auth Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo = '/';
    protected $redirectPath = '/lobby';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

Upvotes: 0

Views: 1038

Answers (1)

Guillermo Lobos
Guillermo Lobos

Reputation: 161

You need to define in your routes the "home" like this

Route::get('/', [   
    'uses' => 'YourController@yourMethod',
    'as' => 'home' //As home 
]);

And when you set that route as 'home' you need to modify the Middleware in RedirectIfAuthenticated.php

public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {
            return redirect()->route('home'); //Redirect to 'home' previously defined in your routes
        }

        return $next($request);
    }

Upvotes: 2

Related Questions