Search code examples
phplaravel-5.3

How do I change the login requirement in Laravel?


I made my laravel application authentication via php artisan make:auth which by default requires you to log in with your email and password.

I need to include another requirement where status == active on my table users. So that for a user to log in the three condations:

  1. Has a valid email
  2. Has a valid password
  3. The status on the users table for that particular use is active (this is activated manually)

So on my login controller I have this:

<?php
namespace App\Http\Controllers\Auth;
use DB;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    // protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password])) 


        {
            // Authentication passed...
            return redirect()->intended('/home');
        }
    }
}

How do insert this condition in the function

  DB::table('users')
    ->where(['status' => active]); For that particular user?

Kindly someone assist.

Thank you.


Solution

  • To 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, 'status' => 'active'])) {
        // The user is active, not suspended, and exists.
    }
    

    Docs