I need to protect them from viewing. That means without logging in it should not redirect to any other pages, if anyone tries to access, it should get back to login page.
I am using Laravel 5.4 and regular authentication
php artisan make:auth
And in login controller
protected $redirectTo = '/home';
And in Middleware RedirectifAuthenticated I am trying like this but it is not working.
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
return redirect('/leadsadd');
return redirect('/leadslist');
return redirect('/opporadd');
return redirect('/opporlist');
return redirect('/accadd');
return redirect('/acclist');
return redirect('/selftask');
}
return $next($request);
}
It's not working. I think the method I am doing is wrong. Can any one help how can I prevent it and should not redirect to any route or URL. Route
Route::get('/', function () {
return view('auth.login');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('leadsadd','crmcontroller@addleads');
Route::get('leadslist', 'crmcontroller@leadslist');
Route::any('leadview/{id}','crmcontroller@show');
Route::get('leadedit/{id}','crmcontroller@edit');
Thanks in advance.
You should group your routes and add the auth
middleware to it. this middleware will automatically redirect to the login page if any non-authenticated user tries to visit any of these pages.
Route::get('/home', 'HomeController@index')->name('home');
Route::middleware(['auth'])->group(function () {
Route::get('leadsadd','crmcontroller@addleads');
Route::get('leadslist', 'crmcontroller@leadslist');
Route::any('leadview/{id}','crmcontroller@show');
Route::get('leadedit/{id}','crmcontroller@edit');
});
You need to do this in your routes like this
Route::group(['middleware' => 'auth'], function () {
// all authenticated users will have the access here!
]]);
and your RedirectIfAuthenticated.php
should be revert back to original
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/yourdashboardorwhatever');
}
return $next($request);
}
There are following two ways to protect your routes in Laravel 5.4
One way is to attach the auth middleware with the route inside your web.php file
Route::get('profile', function () {
// Only authenticated users may enter...
})->middleware('auth');
The other way is to attach the middleware in the constructor of your controller that handles your route.
public function __construct()
{
$this->middleware('auth');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.