Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 496
bmm Avatar asked Jan 30 '26 01:01

bmm


1 Answers

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

like image 183
Amit Gupta Avatar answered Jan 31 '26 14:01

Amit Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!