About laravel 5.2 multi-user authentication function login jump problem
阿神
阿神 2017-05-16 16:53:08
0
3
393

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?

阿神
阿神

闭关修行中......

reply all(3)
仅有的幸福

The jump address should be modified in the middleware

洪涛

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

Peter_Zhu

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');
    });
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!