laravel how to login user with phone number in auth code example
Example 1: 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;
}
}
Example 2: authenticate user with phone laravel
It sounds like you wont be using much of the default AuthController for this as that will expect some form of username/password combo.
You can however use the Auth facade by doing whatever authentication checks you want and then calling the login() method like so:
$user= User::where('password', $password)->get();
if ($user) {
Auth::login($user);
}
This is very bare bones but you can read more here (https: