douju1953 2017-10-16 09:20
浏览 42
已采纳

我如何保护我的laravel身份验证

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.

  • 写回答

3条回答 默认 最新

  • doujin8673 2017-10-16 09:38
    关注

    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');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C