【问题标题】:Override laravel's 5.2 authenticate method in AuthController在 AuthController 中覆盖 laravel 的 5.2 认证方法
【发布时间】:2018-06-09 14:48:19
【问题描述】:

我对 Larvel 仍然很陌生,所以我不知道该怎么做: 我卡在登录系统上。我正在尝试了解如何防止非活动用户使用该应用程序。

编辑

对于非活动用户,我的意思是那些记录字段“活动”为 0 的用户。所以我需要执行一个检查来验证邮件和密码以及活动字段

我已经制作了表格和身份验证系统,并且按照文档中的说明,我已经将此方法放在了我的 AuthController 中:

public function authenticate()
{
   dd('hi');
}

好吧,它完全被忽略了。似乎它永远不会被触发。我错过了什么吗?

我的控制器看起来像原来的 Laravel 5.2 AuthController 除了之前的方法。没有进行其他更改,因为根据文档没有提到其他更改...

我的测试:

我还搜索了一种称为身份验证的方法。但是没有找到方法(使用phpstorm)。所以我的问题是:

如果它是一个特征,不应该声明那个方法吗?所以我可以通过声明一个同名的方法来覆盖它?

重要文件:

Routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', 'HomeController@index');

Route::auth();

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

【问题讨论】:

    标签: php laravel authentication laravel-5 laravel-5.2


    【解决方案1】:

    已解决

    如果您键入 php artisan route:list 您在通过 post 方法登录时看到的,用户将他们的信息帐户发布到 LoginController 中的登录操作。所以,我通过覆盖 Auth\LoginController 中的登录方法解决了这个问题,如下所示:

    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');
        $credentials['active'] = 1;
        if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect()->intended('/');
        }
    }
    

    【讨论】:

      【解决方案2】:

      首先,您需要删除 Auth 路由,但在此之前在您的终端中运行 php artisan route:list。复制与身份验证相关的路由并将它们粘贴到您的路由文件中。

      将每个路由中的控制器更改为指向您自己的应用程序中的 AuthController。

      接下来,将以下 2 个特征添加到您的控制器(页面顶部)和 2 个类中;

      use Illuminate\Foundation\Auth\ThrottlesLogins;
      use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
      use Illuminate\Http\Request;
      use Illuminate\Support\Facades\Auth;
      

      然后在你的类的顶部包含特征;

      use AuthenticatesAndRegistersUsers, ThrottlesLogins;
      

      就目前而言,身份验证功能仍应正常工作。

      然后,更改登录方法以接受活动标志。将以下内容添加到您的新控制器中;

      /**
      * [login description]
      * @param  Request $request [description]
      * @return [type]           [description]
      */
      public function login( Request $request, $guard )
      {
          $this->validateLogin( $request );
          $throttles = $this->isUsingThrottlesLoginsTrait();
      
          if ( $throttles && $lockedOut = $this->hasTooManyLoginAttempts( $request ) ) {
              $this->fireLockoutEvent( $request );
              return $this->sendLockoutResponse( $request );
          }
          $credentials = $this->getCredentials( $request );
          if ( Auth::guard( $guard )->attempt( [ 'active' => 1 ] + $credentials, $request->has( 'remember' ) ) ) {
              return $this->handleUserWasAuthenticated( $request, $throttles, $guard );
          }
      
          // check to see if they can login without the active flag
          if( Auth::guard( $guard )->attempt( $credentials ) ) {
              // log them back out and send them back
              Auth::guard( $guard )->logout();
              return redirect()->back()
                  ->withInput( $request->only( $this->loginUsername(), 'remember' ) )
                  ->withErrors([
                      'active' => 'Your account is currently not active',
                  ]);
          }
          if ( $throttles && ! $lockedOut ) {
              $this->incrementLoginAttempts( $request );
          }
          return $this->sendFailedLoginResponse( $request );
      }
      

      在您自己的控制器中使用这些特征应该会在未来为您提供更大的灵活性。

      问候

      【讨论】:

      • 哇!谢谢!我会这样实现的!
      【解决方案3】:

      为了防止来宾用户(未经过身份验证)访问某些路由,您应该在 routes.php 中保护这些路由:

      // All the routes inside the group are accessible for logged in users only
      Route::group(array('before' => 'auth'), function() {
          Route::get('myroute', 'Controller@action'); 
      });
      
      // For routes accessible for everybody
      Route::get('login', function(){
          return View::make('login');
      });
      

      【讨论】:

      • 嗨!我想我错过了问题的重点。刚刚编辑。我要做的是在登录时执行第三次检查。哪个是记录的活动字段。不确定是否必须在身份验证时进行此检查,或者我只需要在身份验证后检索用户数据并将他阻止在那里...
      • 啊,我明白了。您是否从控制器中删除了特征use AuthenticatesAndRegistersUsers, ThrottlesLogins;?我从这里有完全相同的控制器laravel.com/docs/master/authentication#authenticating-users -authenticate 方法被调用
      • 我之前尝试过,但我收到此错误:Route.php 第 280 行中的 ReflectionException:App\Http\Controllers\Auth\AuthController 类不存在。我认为与路线有某种关系,但我不知道出了什么问题..
      • 你也可以发布你的routes.php吗?
      【解决方案4】:

      已解决

      我自己设法做到了。不确定这是正确的方法,但无论如何它都有效。

      我决定创建一个中间件,在成功登录后,检查用户“活动”字段是否为 1。如果不是,它会重定向到特定页面。

      public function handle($request, Closure $next)
      {
          //  If the user is not logged in
          if(!Auth::user()){
              return redirect('login');
          }
          //  If the user is inactive
          if(!Auth::user()->active){
              return redirect('inactive');
          }
      
          return $next($request);
      }
      

      【讨论】:

        猜你喜欢
        • 2017-04-23
        • 1970-01-01
        • 2020-05-02
        • 2016-07-09
        • 2016-09-21
        • 2019-02-16
        • 2017-01-08
        • 1970-01-01
        • 2016-06-23
        相关资源
        最近更新 更多