Asked  7 Months ago    Answers:  5   Viewed   39 times

I am using Laravel Framework 5.4.10, and I am using the regular authentication that

php artisan make:auth

provides. I want to protect the entire app, and to redirect users to /themes after login.

I have 4 controllers: ForgotPasswordController.php, LoginController.php, RegisterController.php and ResetPasswordController.php. I have edited this line into the last three:

protected $redirectTo = '/themes';

This is the first line in my routes/web.php:

Auth::routes();

I have added this function in my Controller.php:

    public function __construct()
    {
        $this->middleware('auth');

    }

I have edited app/Http/Middleware/RedirectIfAuthenticated.php, so that the handle function looks like this:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/themes');
    }

    return $next($request);
}

It's all fine, except when I click the Login button, I get redirected to "/", not "/themes". If I don't require authentication in the controllers (no __contruct function in Controller.php file), I get redirected OK at login. What am I doing wrong?

 Answers

80

That's what i am currrently working, what a coincidence.

You also need to add the following lines into your LoginController

namespace AppHttpControllersAuth;

use AppHttpControllersController;

use IlluminateFoundationAuthAuthenticatesUsers;

use IlluminateHttpRequest;

class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/

use AuthenticatesUsers;


protected function authenticated(Request $request, $user)
{
if ( $user->isAdmin() ) {// do your magic here
    return redirect()->route('dashboard');
}

 return redirect('/home');
}
/**
 * Where to redirect users after login.
 *
 * @var string
 */
//protected $redirectTo = '/admin';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}
Wednesday, March 31, 2021
 
smiggle
answered 7 Months ago
83

The key was not to use $_SESSION but session(['city' => 'paris']); and session('city') to retrieve the value.

Wednesday, March 31, 2021
 
dimitarvp
answered 7 Months ago
35

You can use a named route to solve this issue:

Lets make a named route like this:

For Get

Route::get('user/additional-fields',array(
    'uses' => 'UserController@getAdditionalFields',
    'as'   => 'user.getAdditionalFields'
));

For post

Route::post('user/additional-fields',array(
    'uses' => 'UserController@postAdditionalFields',
    'as'   => 'user.postAdditionalFields'
));

So we can now ensure Laravel uses the right route by doing this

return redirect()->guest(route('user.getAdditionalFields'));

Also note that its not possible to redirect a POST because Laravel expects form to be submitted. SO you can't do this:

return redirect()->guest(route('user.postAdditionalFields'));

except you use something like cURL or GuzzleHttp simulate a post request

Saturday, May 29, 2021
 
PHLAK
answered 5 Months ago
38

Check If user is authenticated and if user is authenticated then redirect to home

Route::get('/', function () {
   if(Auth::check()){return Redirect::to('home');}
    return view('welcome');
});
Saturday, May 29, 2021
 
peixotorms
answered 5 Months ago
37

After a couple of hours of investigation, I figured out that the solution is quite simple.

All I had to do is to add the following function to nova LoginController:

protected function sendLoginResponse(Request $request)
{
    $request->session()->regenerate();

    $this->clearLoginAttempts($request);

    $redirectPath = $this->redirectPath();
    redirect()->setIntendedUrl($redirectPath);

    return redirect()->intended($redirectPath);
}

Explanation:

This function is implemented in trait AuthenticatesUsers.

intended function (member of Redirector class) create a redirect response, based on previously intended location, and if not exists - on given url.

If no url.intended is set in session, user will be redirected to url generated by LoginController::redirectPath. However, if there an entry of url.intended does exist in session, we need to delete it in order to force redirecting user to the url we are interested in.

The only problem which I see is that we loose the feature of redirecting user to the same page he was before he was logged out. So it is a simple matter of choice as a developer...

Friday, August 6, 2021
 
Thermatix
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :