0
votes

I'll try to explain my issue as much as possible. I just created a Laravel application. Whenever I'm logged in either as a user or admin (user route is http://localhost/Folder/ and admin route is http://localhost/Folder/admin) and I enter the login view path i.e. http://localhost/Folder/login it takes me to this page: http://localhost/Folder/home with error message Page Not Found

I have not added any route of /home in my routes. What I want is on entering the login path, the user should stay on the same page if he is logged in. And the same case for admin instead of going to this path. http://localhost/Folder/home.

Here are the routes for user and admin:

Route::get('/','HomeController@index')->name('home')->middleware(['auth','XSS']);

Route::group(['middleware' => ['auth','admin']], function()
{ Route::get('/admin/dashboard','HomeController@home_page')->name('admin.home');
});

Here is my login controller:

    protected function redirectTo(){
  if (Auth::user()->user_type == 'admin') {
    //return 'admin.dashboard';
    return redirect('/admin/dashboard');
  }
  else {
    //return 'home';
    return redirect('/');
  }

}

And my AdminMiddleware

public function handle($request, Closure $next)
{
  if(Auth::user()->user_type == 'admin') //If usertype is admin
{
   return $next($request);
}
else {
  //  return redirect('home');
    return redirect()->route('/');
}
}

All I want is that it should remain logged in while entering the login page route. I'll be happy to provide any other details if required. Any suggestions/solutions will be highly appreciated.

3
Have you added login routes? I can't see any defined in your code examplesRobertB
I don't think login route is required in web.php file. Correct me if I'm wrongMurtaza Ahmad

3 Answers

1
votes

Edit the route in the RedirectIfAuthenticated middleware

It will redirect to /home if it finds that you are already logged in and try to access the login page.

1
votes

You need to overload redirectTo method on loginController which you have done and update redirectIfAuthenticated to match your logic.

So go-to your loginController and update

protected function redirectTo() {your logic like you have done}

Also you will need to update redirectIfAuthenticated middleware to match your logic

<?php

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;

use Closure;

use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated

{

/**

* Handle an incoming request.

*

* @param \Illuminate\Http\Request $request

* @param \Closure $next

* @param string|null $guard

* @return mixed

*/

public function handle($request, Closure $next, $guard = null)

{

if (Auth::guard($guard)->check()) {

return redirect(RouteServiceProvider::HOME);

}

return $next($request);

}

}

Also if admin/dashboard is always going to be the home route for your auth and admin middleware. Then instead of changing redirectIfAuthenticated.php just change the HOME constant on routeserviceprovider.php in providers and change to

Const HOME = 'admin/dashboard';
0
votes

Thanks everyone for your suggestions. I just tried something and it worked. In the RedirectIfAuthenticated.php file:

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

    return $next($request);
}

Just changed return redirect('/home');

to

return redirect('/');

And it worked...