laravel auth change email to phone for login code example
Example 1: login with email and phone laravel
protected function credentials(Request $request)
{
if(is_numeric($request->get('email'))){
return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
}
elseif (filter_var($request->get('email'), FILTER_VALIDATE_EMAIL)) {
return ['email' => $request->get('email'), 'password'=>$request->get('password')];
}
return ['username' => $request->get('email'), 'password'=>$request->get('password')];
}
Example 2: laravel auth login with phone or email
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function sendFailedLoginResponse(Request $request)
{
throw ValidationException::withMessages([
'username' => [trans('auth.failed')],
]);
}
public function username()
{
$login = request()->input('username');
if(is_numeric($login)){
$field = 'phone';
} elseif (filter_var($login, FILTER_VALIDATE_EMAIL)) {
$field = 'email';
} else {
$field = 'username';
}
request()->merge([$field => $login]);
return $field;
}
}