简体   繁体   English

Laravel,如果用户未通过身份验证,则重定向到特定的登录页面

[英]Laravel, redirect to specific login page if user is not authenticated

I tried all the possible solutions to redirect user if not authenticated to different login pages due to their middleware but every time it takes me to /register, I modified Authenticate.php in middleware folder, as well as the middleware it self but still taking me to /register.我尝试了所有可能的解决方案来重定向用户,如果由于中间件而未通过身份验证到不同的登录页面,但每次带我到 /register 时,我修改了中间件文件夹中的 Authenticate.php,以及它自己的中间件,但仍然带我到/注册。 I want to redirect user to a specific login page if he is not login so I have editor middleware so I want to redirect if someone tried to open the link /editor/dashboard and he is not authenticated the redirect him to /editorlogin如果他没有登录,我想将用户重定向到特定的登录页面,所以我有编辑器中间件,所以如果有人试图打开链接 /editor/dashboard 并且他没有经过身份验证,我想重定向他将他重定向到 /editorlogin

editor middleware:编辑器中间件:

public function handle($request, Closure $next)
{
if(auth()->user() && (auth()->user()->role_id == 3)) {
  return $next($request);   
 } else {
   return redirect()->intended('editorlogin'); 
  }

Route:路线:

 Route::group(['middleware' => ['auth:web','editor'],'prefix' => 'writer'], function () {

 Route::get('/dashboard', 'Writer\WriterController@index')->name('writer.dashboard');



Route::resource('/store', 'Writer\StoreController');

remove auth:web from authauth删除auth:web

web is different midlleware and auth is diffrent web是不同的中间件和auth是不同的

auth has diffrent redirect url that's why you are getting auth redirect url not your custom one auth有不同的重定向 url 这就是为什么你得到auth重定向 url 而不是你自定义的

Route::group(['middleware' => ['web','editor'],'prefix' => 'writer'], function () {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM