first commit
This commit is contained in:
61
app/models/User.php
Normal file
61
app/models/User.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/Database.php';
|
||||
|
||||
class User
|
||||
{
|
||||
public static function findByLogin(string $login): ?array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('SELECT * FROM users WHERE login = :login');
|
||||
$stmt->execute(['login' => $login]);
|
||||
$user = $stmt->fetch();
|
||||
return $user ?: null;
|
||||
}
|
||||
|
||||
public static function verifyPassword(string $login, string $password): ?array
|
||||
{
|
||||
$user = self::findByLogin($login);
|
||||
if ($user && password_verify($password, $user['password'])) {
|
||||
return $user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function saveVerificationCode(int $userId, string $code): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$expiresAt = date('Y-m-d H:i:s', strtotime('+10 minutes'));
|
||||
|
||||
$stmt = $db->prepare('
|
||||
INSERT OR REPLACE INTO verification_codes (user_id, code, expires_at, created_at)
|
||||
VALUES (:user_id, :code, :expires_at, datetime("now"))
|
||||
');
|
||||
|
||||
return $stmt->execute([
|
||||
'user_id' => $userId,
|
||||
'code' => $code,
|
||||
'expires_at' => $expiresAt
|
||||
]);
|
||||
}
|
||||
|
||||
public static function verifyCode(int $userId, string $code): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('
|
||||
SELECT * FROM verification_codes
|
||||
WHERE user_id = :user_id
|
||||
AND code = :code
|
||||
AND expires_at > datetime("now")
|
||||
');
|
||||
$stmt->execute(['user_id' => $userId, 'code' => $code]);
|
||||
return (bool) $stmt->fetch();
|
||||
}
|
||||
|
||||
public static function deleteVerificationCode(int $userId): bool
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare('DELETE FROM verification_codes WHERE user_id = :user_id');
|
||||
return $stmt->execute(['user_id' => $userId]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user