Fixes user tests
parent
0f6dda44b8
commit
58633313e1
|
@ -90,38 +90,34 @@ class User {
|
||||||
* @return bool True if login is successful, false otherwise.
|
* @return bool True if login is successful, false otherwise.
|
||||||
*/
|
*/
|
||||||
public function login($username, $password) {
|
public function login($username, $password) {
|
||||||
try {
|
// Get user's IP address
|
||||||
// Get user's IP address
|
require_once __DIR__ . '/../helpers/logs.php';
|
||||||
require_once __DIR__ . '/../helpers/logs.php';
|
$ipAddress = getUserIP();
|
||||||
$ipAddress = getUserIP();
|
|
||||||
|
|
||||||
// Record attempt
|
// Record attempt
|
||||||
$this->rateLimiter->attempt($username, $ipAddress);
|
$this->rateLimiter->attempt($username, $ipAddress);
|
||||||
|
|
||||||
// Check rate limiting first
|
// Check rate limiting first
|
||||||
if (!$this->rateLimiter->isAllowed($username, $ipAddress)) {
|
if (!$this->rateLimiter->isAllowed($username, $ipAddress)) {
|
||||||
$remainingTime = $this->rateLimiter->getDecayMinutes();
|
$remainingTime = $this->rateLimiter->getDecayMinutes();
|
||||||
throw new Exception("Too many login attempts. Please try again in {$remainingTime} minutes.");
|
throw new Exception("Too many login attempts. Please try again in {$remainingTime} minutes.");
|
||||||
}
|
|
||||||
|
|
||||||
// Then check credentials
|
|
||||||
$query = $this->db->prepare("SELECT * FROM users WHERE username = :username");
|
|
||||||
$query->bindParam(':username', $username);
|
|
||||||
$query->execute();
|
|
||||||
|
|
||||||
$user = $query->fetch(PDO::FETCH_ASSOC);
|
|
||||||
if ($user && password_verify($password, $user['password'])) {
|
|
||||||
$_SESSION['user_id'] = $user['id'];
|
|
||||||
$_SESSION['username'] = $user['username'];
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get remaining attempts AFTER this failed attempt
|
|
||||||
$remainingAttempts = $this->rateLimiter->getRemainingAttempts($username, $ipAddress);
|
|
||||||
throw new Exception("Invalid credentials. {$remainingAttempts} attempts remaining.");
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return $e->getMessage();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Then check credentials
|
||||||
|
$query = $this->db->prepare("SELECT * FROM users WHERE username = :username");
|
||||||
|
$query->bindParam(':username', $username);
|
||||||
|
$query->execute();
|
||||||
|
|
||||||
|
$user = $query->fetch(PDO::FETCH_ASSOC);
|
||||||
|
if ($user && password_verify($password, $user['password'])) {
|
||||||
|
$_SESSION['user_id'] = $user['id'];
|
||||||
|
$_SESSION['username'] = $user['username'];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get remaining attempts AFTER this failed attempt
|
||||||
|
$remainingAttempts = $this->rateLimiter->getRemainingAttempts($username, $ipAddress);
|
||||||
|
throw new Exception("Invalid credentials. {$remainingAttempts} attempts remaining.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue