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?
You can apply this filter to the routes that need authentication. Route::filter('auth', function() { if (Auth::guest()) { return Redirect::guest('login'); } }); What this method basically does it's to store the page you were trying to visit and it is redirects you to the login page. return Redirect::intended();
How can we change this default url to something else? public function auth() { // Authentication Routes... $this->get('login', 'Auth\LoginController@showLoginForm')->name('login'); $this->post('login', 'Auth\LoginController@login'); $this->post('logout', 'Auth\LoginController@logout')->name('logout'); // etc... }
That's what i am currrently working, what a coincidence.
You also need to add the following lines into your LoginController
namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; 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']); } }
If you look in the AuthenticatesUsers trait you will see that in the sendLoginResponse method that there is a call made to $this->redirectPath()
. If you look at this method then you will discover that the redirectTo can either be a method or a variable.
This is what I now have in my auth controller.
public function redirectTo() { $user = Auth::user(); switch(true) { case $user->isInstructor(): return '/instructor'; case $user->isAdmin(): case $user->isSuperAdmin(): return '/admin'; default: return '/account'; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With