在AuthController中覆盖laravel的5.2身份验证方法

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

我对Laravel仍然非常苛刻,所以我无法弄清楚如何做到这一点:我陷入了登录系统。我试图了解如何阻止非活动用户使用该应用程序。

编辑

对于非活动用户,我指的是那些在0上具有“活动”记录字段的用户。所以我需要实现一个检查,它验证邮件和密码以及活动字段

我已经制作了表格和auth系统,并且如文档中所述,我已将此方法放在我的AuthController中:

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

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

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

我的测试:

我还搜索了一个名为authenticate的方法。但没有找到的方法(使用php风暴)。所以我的问题是:

如果它是一个特征,是不是应该声明该方法?所以我只是通过声明一个具有相同名称的方法来覆盖它?

重要文件:

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
4个回答
1
投票

首先,您需要删除Auth路由,但在此之前在终端中运行php artisan route:list。复制与auth相关的路由并将其粘贴在路径文件中。

更改每个路由中的控制器以指向应用程序中的自己的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;

就目前而言,auth功能仍应正常工作。

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

/**
* [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 );
}

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

问候


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('/');
    }
}

0
投票

要防止访客用户(未经过身份验证)访问某些路由,您应该保护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');
});

0
投票

解决了

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

我决定创建一个中间件,在成功登录后,检查用户的“活动”字段是否为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);
}
© www.soinside.com 2019 - 2024. All rights reserved.