Redirecting back to intended laravel 5.5

Published 1 week ago by FutureWeb

I am trying to redirect the user to intended after login so I have taken out the $redirectTo property from the LoginController which is generated by artisan make:auth and added this method:

    protected function redirectTo(){
    
        return redirect()->intended('/home');
    }

But it only ever redirects home ? any help would be great.

Best Answer (As Selected By FutureWeb)
xmarks

Just add the Auth Middleware to your Controller for the specific Method:

/**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // Only Authenticated Users can access Method 1 and 2
        $this->middleware('auth', ['only' => [
            'specificMethod_1', 'specificMethod_2'
        ]]);
    }

Or to all Except a Specific Method:

/**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // Authenticated Users can access any method except specificMethod
        $this->middleware('auth', ['except' => [
            'specificMethod'
        ]]);
    }

Laravel takes care of it out-of-the-box.

If the user is required to be authenticated, they will, and then they will get redirected to where they were previously.

splendidkeen

You could redirect to your intended login page:

// if unsuccessfull, then redirect back to the login with the form data
return redirect()->back()->withInput($request->only('email', 'remember'))->with('info', 'Could not sign in with those details');
FutureWeb

Sorry i wasn't very clear when the user hits a page that requires auth I redirect them to the login form after they have successfully logged in I want to redirect them back to the page they intended to view.

splendidkeen

I see, you could try

return redirect()->intended($this->redirectPath());
FutureWeb

Still no joy :( I just get a blank screen not even a stack trace

splendidkeen

This would be another way // return to previous page

return redirect()->intended(Session::pull('referrer'));
FutureWeb

im getting this now: (after login)

Header may not contain more than a single header, new line detected
FutureWeb

here are the routes that redirect to the login a form is posted to /play then that stores the data in a session and redirects to checkout which checks auth if it fails it redirects to login and then after login it should redirect back to checkout

Route::get('/checkout', function(){

 if(!auth::check())
 {
     return redirect('login');

 }

  $post = Session::get('cart');
  $post = $post[0];
  $post['user_id'] = auth::user()->id;

   return view('checkout', compact('post'));
 });

 Route::post("/play", function(){
   $post = $_POST;
   if(Session::has('cart')) { Session::forget('cart'); }

    Session::push('cart', $post);

    return redirect('checkout');
   });
splendidkeen

Ok, you could may integrate something like

session(['link' => url()->previous()]);
FutureWeb
   $data = \Session::get('_previous');
    $url = $data['url'];
    dd($url);

seems to be what I need

xmarks
xmarks
1 week ago (23,060 XP)

Just add the Auth Middleware to your Controller for the specific Method:

/**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // Only Authenticated Users can access Method 1 and 2
        $this->middleware('auth', ['only' => [
            'specificMethod_1', 'specificMethod_2'
        ]]);
    }

Or to all Except a Specific Method:

/**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // Authenticated Users can access any method except specificMethod
        $this->middleware('auth', ['except' => [
            'specificMethod'
        ]]);
    }

Laravel takes care of it out-of-the-box.

If the user is required to be authenticated, they will, and then they will get redirected to where they were previously.

GertjanRoke

@FutureWeb are you using the original middleware? Because there is where the magic happens

FutureWeb

@GertjanRoke Yep its an out of the box auth:make

Please sign in or create an account to participate in this conversation.