Laravel 5.5多认证路由问题 [英] Laravel 5.5 Multi Authentication Routing Issue

查看:73
本文介绍了Laravel 5.5多认证路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使Laravel多重身份验证可以使用Doctrine而不是Eloquent来工作.我已经尝试了多种方法,但仍会卡住.我目前定义了两个防护,两个模型,两个登录控制器,等等.如果启用一个或另一个,它们将起作用.如果我同时尝试两者,则似乎只有默认防护措施起作用.当我尝试访问其他防护时,我将重定向到错误的登录页面.

Trying to get Laravel multiple authentication to work using Doctrine instead of Eloquent. I've tried multiple things but keep getting stuck. I currently have two guards defined, two models, two login controllers, etc. If I enable either one or the other, they work. If I try both at the same time, only the default guard seems to work. When I try to access the other guard, I get redirected to the wrong login page.

如果我去/login-可以正常工作

If I go to /login - works as expected

如果我去/home(未登录)-如预期那样重定向到/login

If I go to /home (without being logged in) - redirected to /login as expected

如果我去/register-,按预期工作

If I go to /register- works as expected

如果我转到/admin/login-可以正常工作

If I go to /admin/login - works as expected

如果我去/admin/register-可以正常工作

If I go to /admin/register - works as expected

如果我转到/admin(未登录)-失败-应该重定向到/admin/login,而是重定向到/login

If I go to /admin (without being logged in) - fail - should get redirected to /admin/login but instead getting redirected to /login

我确定我缺少一些简单的东西.一切都单独工作.只是让/admin路由使用正确的中间件...我认为...也许吗?

I'm sure I'm missing something simple. Everything works individually. It's just getting the /admin route to use the right middleware...I think...maybe?

我的路线文件:

Auth::routes();

// staff authentication routes
Route::group( [ 'middleware' => [ 'web' ] ], function() {
  Route::get( 'admin/login', 'Auth\StaffLoginController@showLoginForm' );
  Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController@login' ] );
  Route::get( 'admin/register', 'Auth\StaffRegisterController@showRegistrationForm' );
  Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController@register' ] );

  Route::group( [ 'middleware' => [ 'staff' ] ], function() {
    Route::get( '/admin', 'AdminController@index' )->name( 'admin' );
  });
});

Route::get('/home', 'HomeController@index')->name('home');

我尝试了各种路由定义,但这是最新的迭代.以前的迭代均无效.

I've tried various route definitions but this is the latest iteration. None of the previous iterations worked either.

我的身份验证文件:

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

    'staff' => [
        'driver' => 'session',
        'provider' => 'adminusers',
    ]
],

'providers' => [
    'users' => [
        'driver' => 'doctrine',
        'model' => App\Users\Customer::class,
    ],
    'adminusers' => [
        'driver' => 'doctrine',
        'model' => App\Users\Staff::class,
    ]
],

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'adminusers' => [
        'provider' => 'adminusers',
        'table' => 'password_resets',
        'expire' => 30,
    ],
],

我的LoginController(与开箱即用的基本相同):

My LoginController (basically same as out of the box):

class LoginController extends Controller {
  use AuthenticatesUsers;

  protected $redirectTo = '/home';

  public function __construct() {
    $this->middleware('guest')->except('logout');
  }
}

我的StaffLoginController:

My StaffLoginController:

<?php

class StaffLoginController extends Controller {
  use AuthenticatesUsers;

  protected $redirectTo = '/admin';

  protected $guard = 'staff';

  public function __construct() {
    $this->middleware( 'guest' )->except( 'logout' );
  }

  public function showLoginForm() {
    return view( 'auth.staff.login' );
  }

  public function login( Request $request ) {
    $this->validate( $request, [
      'email' => 'required|email',
      'password' => 'required',
    ]);

    if( auth()->guard( 'staff' )->attempt( [
      'email' => $request->input( 'email' ),
      'password' => $request->input( 'password' ),
    ])) {
      return view( 'staff' );
    } else {
      return view( 'auth.staff.login' )->withErrors( [ 'email' => 'Authentication failed' ] );
    }
  }

  protected function guard() {
    return \Auth::guard( 'staff' );
  }
}

我的AdminController:

My AdminController:

class AdminController extends Controller {
  public function __construct() {
    $this->middleware( 'auth:staff' );
  }

  public function index() {
    return view( 'staff' );
  }
}

我的RedirectIfStaffUnauthententated中间件(已在Http \ Kernel.php routeMiddleware中注册为"staff" => \ SNJ \ Http \ Middleware \ RedirectIfStaffUnauthenticated :: class,):

My RedirectIfStaffUnauthenticated middleware (which is registered in Http\Kernel.php routeMiddleware as 'staff' => \SNJ\Http\Middleware\RedirectIfStaffUnauthenticated::class, ):

class RedirectIfStaffUnauthenticated {
  public function handle( $request, Closure $next, $guard = 'staff' ) {
    if ( !Auth::guard( $guard )->check() ) {
      return view( 'auth.staff.login' );
    }

    return $next( $request );
  }
}

更新:

更改了web.php中的路由,使其保持原样(从admin/login和admin/register路由中删除了中间件:

Changed routes in web.php to be as thus (removed the middleware from the admin/login and admin/register routes:

Auth::routes();

// staff authentication routes
Route::get( 'admin/login', 'Auth\StaffLoginController@showLoginForm' );
Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController@login' ] );
Route::get( 'admin/register', 'Auth\StaffRegisterController@showRegistrationForm' );
Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController@register' ] );

Route::group( [ 'middleware' => [ 'staff' ] ], function() {
  Route::get( '/admin', 'AdminController@index' )->name( 'admin' );
});

Route::get('/home', 'HomeController@index')->name('home');

没有变化.仍然不起作用.

No change. Still doesn't work.

因此尝试更改路由(将所有管理路由放入工作人员"中间件: Auth :: routes();

Tried changing routes thusly (put all admin routes into 'staff' middleware: Auth::routes();

// staff authentication routes

Route::group( [ 'middleware' => [ 'staff' ] ], function() {
  Route::get( 'admin/login', 'Auth\StaffLoginController@showLoginForm' );
  Route::post( 'admin/login', [ 'as' => 'staff.login', 'uses' => 'Auth\StaffLoginController@login' ] );
  Route::get( 'admin/register', 'Auth\StaffRegisterController@showRegistrationForm' );
  Route::post( 'admin/register', [ 'as' => 'staff.register', 'uses' => 'Auth\StaffRegisterController@register' ] );
  Route::get( '/admin', 'AdminController@index' )->name( 'admin' );
});

Route::get('/home', 'HomeController@index')->name('home');

相同.仍然不起作用.

推荐答案

将您的StaffLoginController更改为此,

    <?php

class StaffLoginController extends Controller {
  use AuthenticatesUsers;

  public function showLoginForm() {
    return view( 'auth.staff.login' );
  }

  public function login( Request $request ) {
    $this->validate( $request, [
      'email' => 'required|email',
      'password' => 'required',
    ]);

    if( auth()->guard( 'staff' )->attempt( [
      'email' => $request->input( 'email' ),
      'password' => $request->input( 'password' ),
    ])) {
      return view( 'staff' );
    } else {
      return view( 'auth.staff.login' )->withErrors( [ 'email' => 'Authentication failed' ] );
    }
  }
}

从AdminController中删除构造函数,我们稍后将在路由上调用此中间件.

remove constructor from AdminController, we are later going to call this middleware on routes.

class AdminController extends Controller {

  public function index() {
    return view( 'staff' );
  }
}

您不需要将保护值传递给auth中间件,因为您已将其定义为用于验证admin路由的第二个中间件.

You don't need to pass the guard value to auth middleware since you already defined as second middleware for authenticating admin routes.

像这样更新您的员工中间件.

update your staff middleware like this.

class RedirectIfStaffUnauthenticated {
  public function handle( $request, Closure $next, $guard = null ) {
    if ( Auth::guard( $guard )->check() ) {

if(! $guard == 'staff')
                {
                    return redirect()->route( 'staff.login.form' ); 
                }
     }else return redirect()->route( 'staff.login.form' ); 

    return $next( $request );
  }
}

现在更新您的路线.

Route::get( 'admin/login', 'Auth\StaffLoginController@showLoginForm' );
Route::post( 'admin/login', [ 'as' => 'staff.login.submit', 'uses' => 'Auth\StaffLoginController@login' ] );
Route::get( 'admin/register', 'Auth\StaffRegisterController@showRegistrationForm' );
Route::post( 'admin/register', [ 'as' => 'staff.register.submit', 'uses' => 'Auth\StaffRegisterController@register' ] );
Route::group( [ 'middleware' => [ 'staff' ] ], function() {
  Route::get( '/admin', 'AdminController@index' )->name( 'admin' );
});

还要在app\ExceptionsHandler.php文件的unauthenticated函数中添加以下几行

Also add the following lines to your unauthenticated function in Handler.php file in app\Exceptions

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

请检查您的app/Kernel.php,您会看到guestapp/Http/Middleware/RedirectIfAuthenticated.php中间件的别名.

Please check your app/Kernel.php, you can see that guest is an alias for app/Http/Middleware/RedirectIfAuthenticated.php middleware.

您无需同时在web.php文件和控制器构造函数上调用构造函数.

You don't need to call constructors on both web.php file and on the controllers constructor.

RedirectIfStaffUnauthenticated中间件在这里检查根/admin是否已通过身份验证,并且其防护值staff,否则将重定向到/admin/login路由.

Here the RedirectIfStaffUnauthenticated middleware checks the root /admin is authenticated and it has a guard value of staff, if not it will redirect to the /admin/login route.

请阅读 laravel文档进行澄清

希望这会有所帮助.

这篇关于Laravel 5.5多认证路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆