Support the ongoing development of Laravel.io →
Authentication Validation

I have a login form that accepts either an email or a phone number (or maybe more in the future) in a single field as part of the credentials (similar to how Facebook allows "Email or Phone").

  • Is it possible to create a validation rule that checks the existence of one @input against two fields (@email, @phone) in the database?
where (email = @input or phone = @input)
  • And is it possible for Auth to authenticate this kind of request?
where (email = @input or phone = @input) and password = @password

I know I can probably customize all of this, but it would be nice to know that such facility exists.

Last updated 2 years ago.
0

try that ...

public function loginpost(Request $request)
{
       
  $field = filter_var($request->input('email'), FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';

   if(Auth::attempt([$field=> $request->input('email'), 'password' => $request->input('password')], true))
     {
              return 'you are logged in';
      }
       else
       {
             return 'error';
        }
        
    }
Last updated 9 years ago.
0

Thanks! That takes care of part of my problem.

I currently have a more verbose solution in place at the moment. Granted its not very elegant, but it works for now, and fulfils my workflow / response requirements.

$email = Validator::make($request->all(), [
    'identity' => 'required|email|exists:users,email|min:5|max:100',
]);

$phone = Validator::make($request->all(), [
    'identity' => 'required|regex:@\+\d{1,3}\s{1}\d{3,4}\s{1}\d{6,8}@|exists:users,phone|min:5|max:100',
]);

$password = Validator::make($request->all(), [
    'password' => 'required|min:5|max:100',
]);

if ($email->passes() && $password->passes())
{
    // Attempt Auth using email and password
}
elseif ($phone->passes() && $password->passes())
{
    // Attempt Auth using phone and password
}
else
{
    // Validation failed
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

schajee schajee Joined 11 Oct 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.