Caius Caius - 3 months ago 47
PHP Question

Override laravel's 5.2 authenticate method in AuthController

I'm still very noobish with Larvel, so i can't figure out how to do this:
I'm stuck at the login system. I'm trying to understand how i can prevent inactive users to use the application.

Edit



With inactive users i mean those users which has the record field 'active' on 0. So i need to implement a check which verifies the mail and the password and also the active field

I've made the tables and the auth system, and as written in the documentation, I've placed this method inside my AuthController:

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


Well it completely gets ignored. It seems like it never gets triggered. Am I missing something?

My controller looks like the original Laravel's 5.2 AuthController except for the method before. No other changes has been made, since according to the documentation no other changes are mentioned...

My tests:



I've also searched for a method called authenticate. But no methods where found (using php storm). So my question is:

If it's a trait, isn't supposed to have that method declared? So i can override it just by declaring a method with the same name?

Important files:



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');

Answer

Firstly you need to remove the Auth routes but before that run php artisan route:list in your terminal. Copy the auth related routes and stick them in your routes file.

Change the controller in each of the routes to point to your own AuthController in your application.

Next, include the following 2 traits into your controller (top of the page) plus 2 classes;

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

and then include the traits in the top of your class;

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

as it stands, the auth functionality should still work as normal.

The to alter the login method to accept an active flag. Add the following to your new controller;

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

using the traits in your own controller should give you more flexibility in the future.

regards