Igor da Silva Coelho
Igor da Silva Coelho

Reputation: 57

Laravel Auth fail with duplicated user's email

My application needs to store the same user many times. That's because when a user is removed, I make a logical exclusion, just changing a status field in the user table. However, if I have two users with the same email, Laravel Auth fails. How do I can solve this problem? How Can I customize auth to consider this status field?

Upvotes: 3

Views: 647

Answers (2)

Faid
Faid

Reputation: 605

In LoginController you can override credentials method which exists in AuthenticatesUsers trait

/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function credentials(Request $request)
{
    // return ['email' => .., 'password' => .., 'status' => 1];

    return $request->only($this->username(), 'password');
}

Upvotes: 0

Vikash Pathak
Vikash Pathak

Reputation: 3562

You can add custom condition like:

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

Upvotes: 4

Related Questions