Laravel 7:重定向到不同守卫的不同登录

问题描述 投票:0回答:4

我正在使用身份验证中间件并创建管理防护来控制管理员访问。当我尝试访问管理路由时遇到一些问题,我想将与管理路由关联的未经身份验证的流量重定向到 /admin/login 页面,但它没有这样做,而是将我重定向到 /login 页面。 我不知道如何获取与 Authenticate 类中的路线关联的守卫。

 protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

这就是代码,我希望是这样的:

 protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            if(Auth::guard('admin'))
               return route('admin.login);
            else
               return route(login);
        }
    }
}

但它不起作用,因为我唯一的参数是 $request。

这些是我的路线...


//Admin Routes
Route::middleware(['auth:admin'])->group(function () {
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/newDoncente', 'AdminController@addDocenteView')->name('newDocente');

//Docentes Routes
Route::get('/docentes', 'Docente\DocenteController@getDocentesView')->name('getDocentesView');
Route::get('/editDocente/{id}', 'Docente\DocenteController@editDocenteView')->name('editDocentesView');
Route::get('/docentesTables', 'Docente\DocenteController@getDocentesDatatables')->name('getDocentesTables');
Route::get('/docente/{id}', 'Docente\DocenteController@getDocenteView')->name('getDocenteView');

谢谢。

php laravel authentication laravel-7 guard
4个回答
5
投票

我一直在寻找基于守卫的答案,我希望检查守卫而不是基于守卫类型的重定向。但是我找不到解决方案。

但是,我找到了另一个可以完成工作的解决方案,但它并不像我正在寻找的答案那么动态。不过,对我来说效果很好

App\Http\Middleware\Authinticate.php

类,添加以下代码:

protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            if($request->is('admin') || $request->is('admin/*'))
            return route('admin.login');
            
            return route('login');
        }
    }

另外,不要忘记为所有管理路由添加前缀 admin/ 。 希望这个回答对你有帮助。


4
投票

转到应用程序/Exception/Handler.php

使用这个类

use Illuminate\Auth\AuthenticationException;

并在类中添加这个函数


    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if (request()->expectsJson()) {
            return Response()->json(['error' => 'UnAuthorized'], 401); //exeption for api
        }

        $guard = data_get($exception->guards(), 0);
        switch ($guard) {
            case 'admin':
                $login = 'admin.login';
                break;
            default:
                $login = 'login';
                break;
        }
        return redirect()->guest(route($login));
    }

4
投票

我重写了 handle 方法,这样我就可以从中获取守卫,如下所示:

protected $guards = [];

public function handle($request, Closure $next, ...$guards)
{
    $this->guards = $guards;

    return parent::handle($request, $next, ...$guards);
}
protected function redirectTo($request)
{
    if (in_array("admin", $this->guards)) {
        return "admin/login";
    }

}

0
投票

好的,我已经解决了,我想这是一个很好的方法。我创建了一个中间件,并要求中间件上的身份验证防护。

首先我创建中间件


class IsAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
    
        if (Auth::guard('admin')->check()) 
        return $next($request);
        else 
        return redirect('/admin/login');

    }
}

然后我把中间件放到了Kernel文件中

 protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'isAdmin' => \App\Http\Middleware\IsAdmin::class,

最后,我更改了路线的中间件

Route::get('/admin', 'AdminController@index')->name('admin');

现在工作正常,但如果你有更好的解决方案,我真的很想知道。

© www.soinside.com 2019 - 2024. All rights reserved.