Where to find the Auth::attempt method in laravel

Viewed 9992

I want to add extra conditions to the authentication query in addition to the user's e-mail and password in my Laravel project

I read this on the officials laravel documentation. https://laravel.com/docs/5.6/authentication

Specifying Additional Conditions If you wish, you may also add extra conditions to the authentication query in addition to the user's e-mail and password. For example, we may verify that user is marked as "active":

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
    // The user is active, not suspended, and exists.
}

Where can I find this method?

4 Answers

You need overwriyte method login in your LoginController

public function login(\Illuminate\Http\Request $request) {
    $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);
        return $this->sendLockoutResponse($request);
    }

    // This section is the only change
    if ($this->guard()->validate($this->credentials($request))) {
        $user = $this->guard()->getLastAttempted();

        // Make sure the user is active
        if ($user->active && $this->attemptLogin($request)) {
            // Send the normal successful login response
            return $this->sendLoginResponse($request);
        } else {
            // Increment the failed login attempts and redirect back to the
            // login form with an error message.
            $this->incrementLoginAttempts($request);
            return redirect()
                ->back()
                ->withInput($request->only($this->username(), 'remember'))
                ->withErrors(['active' => 'You must be active to login.']);
        }
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}

This working for me

Auth::attempt that you are calling is most likely Illuminate\Auth\SessionGuard@attempt.

The path:

Auth::attempt -> Illuminate\Auth\AuthManager -> (guard) Illuminate\Auth\SessionGuard

Facade -> Illuminate\Auth\AuthManager@call -> @guard -> Illuminate\Auth\SessionGuard@attempt

Laravel 5.6 Docs - Facades - Class Reference

For being able to adjust those credentials passed for the LoginController you only need to override 1 method, credentials, as it is the only method involved with the array that is passed to attempt.

protected function credentials(Request $request)
{
    return $request->only($this->username(), 'password')
        + ['active' => true];
}

If you use the auth scaffolding provided by Laravel, it will be in the AuthenticatesUsers trait:

/**
 * Attempt to log the user into the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return bool
 */
protected function attemptLogin(Request $request)
{
    return $this->guard()->attempt(
        $this->credentials($request), $request->filled('remember')
    );
}

You can override this method in your LoginController.

AuthController.php

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Routing\Controller;

class AuthController extends Controller
{
/**
 * Handle an authentication attempt.
 *
 * @return Response
 */
public function authenticate()
{
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}
}

Is this the one that you are looking for?

Related