search

Home  >  Q&A  >  body text

About laravel 5.2 multi-user authentication function login jump problem

Refer to the tutorial http://laravelacademy.org/post/3502.html when doing front-end and back-end user authentication
I encountered a problem that when a non-logged-in user accesses /admin, it should jump to / admin/login, but I don’t know why it always jumps to /login. Please ask me what is the reason?

阿神阿神2780 days ago465

reply all(3)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-16 16:55:08

    The jump address should be modified in the middleware

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:55:08

    Authenticate under AppHttpMiddleware
    return redirect()->guest('admin/login');
    That’s it

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 16:55:08

    Authenticate middleware

    public function handle($request, Closure $next, $guard = null)
        {
            if (Auth::guard($guard)->guest()) {
                if ($request->ajax()) {
                    return response('Unauthorized.', 401);
                } else {
                    return redirect()->guest($guard.'/login'); //注意这里的$guard
                }
            }
    
            return $next($request);
        }

    route.php

    Route::group(['middleware' => ['auth:admin']], function () { // auth:admin 调用auth中间件的时候传递一个admin,这个admin正好是被中间件的$guard接收,于是访问后台的时候都会被跳转导admin/login,同理 前台用户登陆的中间件可以是 auth:user , 这样会跳转到user/login
    
            Route::get('admin/index', 'AdminController@index');
        });

    reply
    0
  • Cancelreply