authentication via username or phones

This commit is contained in:
2024-09-18 17:43:33 +05:00
parent e95797bb97
commit c086feb6c0
10 changed files with 78 additions and 25 deletions

View File

@@ -2,29 +2,25 @@
namespace App\Http\Controllers\Auth;
use App\Models\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Nova\Nova;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers, ValidatesRequests;
/**
* Username
*/
protected string $username = 'username';
/**
* Create a new controller instance.
*
@@ -99,7 +95,7 @@ class LoginController extends Controller
*/
public function username(): string
{
return 'username';
return $this->username;
}
/**
@@ -117,6 +113,54 @@ class LoginController extends Controller
]);
}
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$user = User::where('username', $request->username)
->orWhere('phone', unMaskPhone($request->username))
->first();
if (! $user) {
return $this->sendFailedLoginResponse($request);
}
if (Hash::check($request->password, $user->password)) {
auth()->login($user);
if ($request->hasSession()) {
$request->session()->put('auth.password_confirmed_at', time());
}
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
/**
* Send the response after the user was authenticated.
*