I'm just starting out after generating a fresh laravel app using artisan
. By following instructions available in the official docs about Authentication, I've added forms for login and registration.
Since I want user to be redirected to the /dashboard
route after authentication, I've added the following to my AuthController
:
protected $this->redirectPath = '/dashboard';
protected $this->redirectTo = '/dashboard';
Things seem for work as expected. After a successful login, user is taken to the /dashboard
.
The problem happens when a user has already logged in (using the /auth/login
route) and is again navigating to the /auth/login
route through the browser's address bar. In such cases, laravel is redirecting the user back to /home
, a route which of course, is not defined in my app. So, I get the following error displayed in the browser:
NotFoundHttpException in RouteCollection.php line 143:
in RouteCollection.php line 143
at RouteCollection->match(object(Request)) in Router.php line 746
at Router->findRoute(object(Request)) in Router.php line 655
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 237
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
I would have expected the user to be taken directly to the dashboard
. Any help is appreciated.
Found a work around which I'm not sure is the right thing to do. I realized that my getLogin()
isn't even called if the user is logged in, (I don't know why). So this is what I did.
In the AuthController.php\__construct()
:
if (\Auth::check())
{
return redirect()->intended('/dashboard');
}
and same two lines in the AuthController.php\getLogin()
:
if (\Auth::check())
{
return redirect()->intended('/dashboard');
}
Hope this helps.
You have to change the redirect value in handle method of app\Http\Middleware\RedirectIfAuthenticated.php
public function handle($request, Closure $next)
{
if ($this->auth->check()) {
return redirect('/home');
}
return $next($request);
}
it worked for me .apply this logic:
Route::get('/', ['middleware' => 'auth', function (){
if(Auth::check())
{
return redirect()->action('BrandController@index');
}
}]);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community