Jahid Mahmud
Jahid Mahmud

Reputation: 1136

Laravel Authentication not working as secifically in laravel 5.3

I am stuck in my project. I want that the user can login with email or mobile no. This works perfectly fine. But now I want that only the active user can login with email or mobile no. I have a active field in my database which is set to 1 if a user is active or if a user is inactive it sets to 0. But dont know how to do this. Please guys help me solving this. I am using laravel 5.3.

My LoginController code is

<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/home';
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }
    protected $username = 'email';
    public function loginNameOrEmail(Request $request)
    {
        $field = filter_var($request->input('email'), FILTER_VALIDATE_EMAIL) ? 'email' : 'mobile';
        $request->merge([$field => $request->input('email')]);
        $this->username = $field;
        return $this->login($request);    
    }
    public function username()
    {
        return $this->username;
    }
    protected function authenticated($request, $user)
    {
        if($user->is_admin == 1) {
            return redirect()->intended('dashboard');
        }
        return redirect()->intended('/home');
    }
}

Upvotes: 1

Views: 334

Answers (1)

Amit Gupta
Amit Gupta

Reputation: 17668

You can 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.
}

Update

The simplest way is to override the credentials method as:

protected function credentials(Request $request) {

        $request['active'] = 1;
        return $request->only($this->username(), 'password', 'active');
}

Add this method in your LoginController.

Docs

Upvotes: 1

Related Questions