Laravel - Login with username, email or phone

Laravel - Login with username, email or phone

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel - Login with username, email or phone

Implementing authentication that accepts multiple identifiers—username, email, or phone number—is a common requirement for modern applications. While the default AuthenticatesUsers trait in Laravel handles basic login flows elegantly, extending it to support three distinct fields requires adjusting how credentials are resolved and validated against your database.

The core challenge lies in the credentials() method within your LoginController. This method is responsible for determining which column in your users table to query based on the input provided. In standard implementations, you typically check if the input matches an email format using filter_var with FILTER_VALIDATE_EMAIL. If it does, Laravel queries the email column; otherwise, it falls back to the username column.

To add phone number support, you must refine this logic to handle three cases simultaneously. The most robust approach involves checking the input against a pattern for both email and numeric strings before falling back to the username field.

Here is an updated implementation of the credentials method that supports all three identifiers:

use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    /**
     * Get the needed authorization credentials from the request.
     */
    protected function credentials(Request $request)
    {
        $usernameField = $this->username(); // Typically 'username' or 'email' depending on your setup
        
        $inputIdentifier = $request->only($usernameField, 'password');

        // Check if input is a valid email
        if (filter_var($inputIdentifier[$usernameField], FILTER_VALIDATE_EMAIL)) {
            $credentials = [
                'email' => $inputIdentifier[$usernameField],
                'password' => $inputIdentifier['password'],
            ];
        } 
        // Check if input is a valid phone number (adjust regex based on your storage format)
        elseif (preg_match('/^\d{10,}$/', trim($inputIdentifier[$usernameField]))) {
            $credentials = [
                'phone' => $inputIdentifier[$usernameField],
                'password' => $inputIdentifier['password'],
            ];
        } 
        // Fallback to username
        else {
            $credentials = [
                'username' => $inputIdentifier[$usernameField],
                'password' => $inputIdentifier['password'],
            ];
        }

        return $credentials;
    }

    /**
     * Validate a login request.
     */
    public function validate(Request $request)
    {
        $request->validate([
            $this->username() => ['required', 'string', 'max:255'],
            'password' => ['required', 'string'],
        ]);
    }
}

Database Schema Considerations

For this logic to function, your users table must contain columns for all three potential identifiers. Based on the provided source material, the registration controller already stores these fields:

return User::create([
    'name' => $data['name'],
    'gender' => $data['gender'],
    'username' => $data['username'],
    'phone' => $data['phone'],
    'email' => $data['email'],
    // ... other fields
]);

Ensure that your database indexes are optimized for these lookups. Querying by phone number or email is generally faster than querying by username if the users table grows large, as usernames can be less unique (though your schema enforces uniqueness). However, since you are enforcing uniqueness on all three columns (unique:users), any of them will perform efficiently with an index.

Security and Validation Trade-offs

A critical pitfall in multi-field authentication is ensuring that the phone field is stored consistently. In your provided RegisterController, the phone number is saved directly as a string. If you store it with formatting (e.g., "+1 555-0199") but expect to login with just digits ("5550199"), the validation regex in the login controller will fail.

Normalize your data during registration by stripping special characters before saving:

// In RegisterController::create()
$phone = preg_replace('/[^0-9]/', '', $data['phone']); // Remove non-digits
return User::create([
    'phone' => $phone,
    // ...
]);

Additionally, ensure your LoginController handles the case where a user might not have a phone number defined in their record. The logic above assumes every row has all three columns. If you use nullable columns for phone and username, the query builder will still work, but be aware that an empty string check is necessary if your application allows users to omit certain identifiers entirely during registration (which your current required validators prevent).

By implementing this conditional logic in the credentials() method and ensuring data consistency at the point of registration, you create a seamless login experience where users can authenticate using whichever identifier they prefer.

Our network