I need after check if user is logged as editor, to redirect to profile page...
Here is my code:
<?php namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Closure;
class AdminMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check()){
if(Auth::user()->roles->toArray()[0]['role'] == 'editor'){
return redirect('/profile');
}
return $next($request);
}
else
{
return $next($request);
}
}
}
Problem with this code is when user is editor I get infinite loop....
Here is my routs:
Route::group(['middleware' => 'auth'], function(){
Route::get('home', ['middleware' => 'admin', function()
{
return view('home');
}]);
Route::get('profile', array(
'as' => 'profile',
'uses' => 'UserController@getProfile'
));
});
Anyone know what is problem?
By default laravel will redirect a user to the home page like so: protected $redirectTo = '/home'; To change that default behaviour, add the following code in App/Http/Controllers/Auth/LoginController. php . This will redirect users (after login) to where ever you want.
Laravel offers an easy way to redirect a user to a specific page. By using the redirect() helper you can easily create a redirect like the following: return redirect('/home'); This will redirect the user to the /home page of your app.
You may use Redirect::intended function. It will redirect the user to the URL they were trying to access before being caught by the authenticaton filter. A fallback URI may be given to this method in case the intended destinaton is not available.
If you just want to redirect a user back to the previous page (the most common example - is to redirect back to the form page after data validation failed), you can use this: return redirect()->back();
I found this to be a less code and less decisions for redirecting users based on roles, Put this in your AuthController.php
protected function authenticated( $user)
{
if($user->user_group == '0') {
return redirect('/dashboard');
}
return redirect('my-account');
}
https://laracasts.com/discuss/channels/laravel/how-best-to-redirect-admins-from-users-after-login-authentication
Where did you register your middleware in App\Http\Kernel
?
Is it in protected $middleware = []
or protected $routeMiddleware = []
?
If registered in $middleware
it will run on each very request thereby causing infinite loop, if so use only $routeMiddleware
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