File manager - Edit - /home/webapp69.cm.in.th/u69319090039/Shop39/public/app/Controllers/AuthController.php
Back
<?php class AuthController extends Controller { public function showLogin(): void { if (Auth::check()) { $this->redirect(''); } $this->render('auth/login', ['title' => 'เข้าสู่ระบบ']); } public function login(): void { $this->validateCsrf(); $username = Security::sanitizeString($_POST['username'] ?? ''); $password = $_POST['password'] ?? ''; if (empty($username) || empty($password)) { Session::setFlash('danger', 'กรุณากรอกชื่อผู้ใช้และรหัสผ่านให้ครบถ้วน'); $this->redirect('login'); } $userModel = new User(); // Allow login via username or email $user = $userModel->findByUsername($username) ?? $userModel->findByEmail($username); if ($user && Security::verifyPassword($password, $user['password'])) { if ($user['status'] !== 'active') { Session::setFlash('danger', 'บัญชีผู้ใช้ของคุณถูกระงับหรือยังไม่อนุมัติ'); $this->redirect('login'); } Session::set('user_id', $user['id']); Session::setFlash('success', 'เข้าสู่ระบบสำเร็จ! ยินดีต้อนรับคุณ ' . $user['full_name']); if ($user['role'] === 'admin') { $this->redirect('admin/dashboard'); } else { $this->redirect(''); } } else { Session::setFlash('danger', 'ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง'); $this->redirect('login'); } } public function showRegister(): void { if (Auth::check()) { $this->redirect(''); } $this->render('auth/register', ['title' => 'สมัครสมาชิก']); } public function register(): void { $this->validateCsrf(); $username = Security::sanitizeString($_POST['username'] ?? ''); $email = Security::sanitizeEmail($_POST['email'] ?? ''); $password = $_POST['password'] ?? ''; $fullName = Security::sanitizeString($_POST['full_name'] ?? ''); $phone = Security::sanitizeString($_POST['phone'] ?? ''); if (empty($username) || empty($email) || empty($password) || empty($fullName)) { Session::setFlash('danger', 'กรุณากรอกข้อมูลที่จำเป็นให้ครบถ้วน'); $this->redirect('register'); } $userModel = new User(); if ($userModel->findByUsername($username)) { Session::setFlash('danger', 'ชื่อผู้ใช้นี้ถูกใช้งานแล้ว'); $this->redirect('register'); } if ($userModel->findByEmail($email)) { Session::setFlash('danger', 'อีเมลนี้ถูกใช้งานแล้ว'); $this->redirect('register'); } $userId = $userModel->create([ 'username' => $username, 'email' => $email, 'password' => Security::hashPassword($password), 'full_name' => $fullName, 'phone' => $phone, 'role' => 'customer', 'status' => 'active' ]); Session::set('user_id', $userId); Session::setFlash('success', 'สมัครสมาชิกสำเร็จ! เข้าสู่ระบบเรียบร้อยแล้ว'); $this->redirect(''); } public function logout(): void { Session::destroy(); Session::init(); Session::setFlash('success', 'ออกจากระบบเรียบร้อยแล้ว'); $this->redirect('login'); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.25 |
proxy
|
phpinfo
|
Settings