diff --git a/src/Auth.php b/src/Auth.php index dc5c987..765e46e 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -60,21 +60,44 @@ class Auth } /** - * 通过手机号 + 密码尝试登录 + * 尝试登录。账号可以是**手机号**,也可以是**姓名**。 + * + * 为什么两种都收:安装向导创建的管理员填的是手机号,而家人账号常常不填手机号 + * (只有名字),演示/内网场景下用名字登录更自然。 + * + * 顺序:先按手机号 hash 精确匹配(唯一,不会歧义),再按姓名匹配。 + * 姓名可能重名 ⇒ **命中多条一律判失败**,绝不「取第一条」—— + * 那等于让 A 用密码登进了 B 的账号,是最坏的一种 bug。 + * * @return array|null 成功返回用户数组,失败返回 null */ - public static function attempt(string $phone, string $password): ?array + public static function attempt(string $account, string $password): ?array { - $phone = trim($phone); - if ($phone === '' || $password === '') return null; + $account = trim($account); + if ($account === '' || $password === '') return null; - $phoneHash = hash('sha256', $phone); - $stmt = Database::get()->prepare( + $db = Database::get(); + + // ① 手机号:走 sha256 精确查(唯一,不会歧义) + $stmt = $db->prepare( 'SELECT * FROM users WHERE phone_hash = ? AND role IN ("admin","user") AND status = "active" LIMIT 1' ); - $stmt->execute([$phoneHash]); + $stmt->execute([hash('sha256', $account)]); $user = $stmt->fetch(); - if (!$user) return null; + + // ② 手机号没命中,再按姓名查 + if (!$user) { + $stmt = $db->prepare( + 'SELECT * FROM users WHERE name = ? AND role IN ("admin","user") AND status = "active"' + ); + $stmt->execute([$account]); + $rows = $stmt->fetchAll(); + + // 重名 → 拒绝,不做猜测 + if (count($rows) !== 1) return null; + $user = $rows[0]; + } + if (!password_verify($password, (string)($user['password_hash'] ?? ''))) return null; return $user; diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index 4a3bdcb..d51b1e5 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -24,7 +24,7 @@ class AuthController $user = Auth::attempt($phone, $password); if (!$user) { - flash('error', '手机号或密码错误'); + flash('error', '账号或密码错误'); redirect('auth/login'); } diff --git a/templates/auth/login.php b/templates/auth/login.php index d4e567c..58cf0d0 100644 --- a/templates/auth/login.php +++ b/templates/auth/login.php @@ -5,8 +5,8 @@
请登录后继续