0

在我使用 Laravel 8 和 vue+惯性 + fortify 包创建的项目中,我使用了两个守卫,一个用于普通用户,一个用于管理员。但只有一个登录视图重定向。

我只想向普通用户显示不同的登录名,向管理员显示另一个登录名。它应该由路由中使用的中间件检测。我可以过滤它,如果我能从那里得到请求的警卫名称。

这是我的示例路线:

<?php
//'auth:users' is normal users guard

 Route::group(['middleware' => 'auth:users'], function () {
            Route::prefix('/account')->name('account.')->group(function () {
                Route::get('/', [AccountController::class, 'index'])->name('index');
});});

//'auth:web' is admin users guard

 Route::group(['middleware' => 'auth:web'], function () {
      Route::prefix('/admin')->name('admin.')->group(function () {
            Route::prefix('/account')->name('account.')->group(function () {
                Route::get('/', [AdminAccountController::class, 'index'])->name('index');
   });
 });
});

身份验证中间件:

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        // both request coming to here i want get middlware name from here
   //       if('auth:web'){
 //redirect to adimin login
 //         }else{
 //redirect to userlogin
 //}

       
        if (! $request->expectsJson()) {
            return route('admin.login');
        }
    }
}
4

1 回答 1

0

您可以使用以下方式获取用于当前路由的所有中间件的列表request()->route()->computedMiddleware

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if ($request->route() && in_array('auth:web', $request->route()->computedMiddleware??[]) {
         // redirect to admin login
       } else {
         // redirect to admin login
       }
 
       
        if (! $request->expectsJson()) {
            return route('admin.login');
        }
    }
}
于 2021-03-31T08:30:10.167 回答