File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Controllers/AuthController.php
Back
<?php /** * AuthController * Handles user registration, login, logout, and token-based password reset */ require_once __DIR__ . '/../Core/Controller.php'; require_once __DIR__ . '/../Services/AuthService.php'; require_once __DIR__ . '/../Models/UserAgreement.php'; require_once __DIR__ . '/../Helpers/Session.php'; require_once __DIR__ . '/../Helpers/Security.php'; require_once __DIR__ . '/../Helpers/Url.php'; class AuthController extends Controller { private AuthService $authService; private UserAgreement $agreementModel; public function __construct() { $this->authService = new AuthService(); $this->agreementModel = new UserAgreement(); } public function showLogin(): void { $this->render('auth/login', [ 'pageTitle' => __('login_title') . ' - CARGOO', 'authColClass' => 'auth-container-login' ], 'auth'); } public function login(): void { $identifier = $_POST['identifier'] ?? ''; $password = $_POST['password'] ?? ''; $rememberMe = !empty($_POST['remember_me']); $result = $this->authService->authenticate($identifier, $password, $rememberMe); if (!$result['success']) { Session::setOldInput($_POST); Session::setFlash('danger', $result['message']); $this->redirect('/login'); } // Redirect based on roles $roles = $result['roles'] ?? []; if (in_array('admin', $roles, true) || in_array('operation_admin', $roles, true)) { // If user has admin role $this->redirect('/account/profile'); } else { $this->redirect('/account/profile'); } } public function showRegister(): void { $agreement = $this->agreementModel->getLatestActive('customer'); $this->render('auth/register', [ 'pageTitle' => __('create_account') . ' - CARGOO', 'agreement' => $agreement, 'authColClass' => 'auth-container-register' ], 'auth'); } public function register(): void { $result = $this->authService->register($_POST); if (!$result['success']) { Session::setOldInput($_POST); foreach ($result['errors'] as $field => $errorMsg) { Session::setFlash('danger', $errorMsg); } $this->redirect('/register'); } Session::clearOldInput(); Session::setFlash('success', __('register_success')); $this->redirect('/login'); } public function logout(): void { $this->authService->logout(); Session::setFlash('success', __('logout_success')); $this->redirect('/login'); } public function showForgotPassword(): void { $this->render('auth/forgot_password', [ 'pageTitle' => __('forgot_password_title') . ' - CARGOO', 'authColClass' => 'auth-container-login' ], 'auth'); } public function forgotPassword(): void { $identifier = $_POST['identifier'] ?? ''; if (empty($identifier)) { Session::setFlash('danger', __('field_required')); $this->redirect('/forgot-password'); } $result = $this->authService->requestPasswordReset($identifier); // Flash message Session::setFlash('success', $result['message']); // For local development / demo convenience if reset link is present: if (!empty($result['reset_link'])) { Session::setFlash('info', 'ลิงก์รีเซ็ตรหัสผ่านสำหรับทดสอบ (Demo): <a href="' . $result['reset_link'] . '" class="alert-link text-decoration-underline">คลิกที่นี่เพื่อตั้งรหัสผ่านใหม่</a>'); } $this->redirect('/forgot-password'); } public function showResetPassword(): void { $token = $_GET['token'] ?? ''; if (empty($token)) { Session::setFlash('danger', __('invalid_or_expired_token')); $this->redirect('/forgot-password'); } $resetTokenModel = new PasswordResetToken(); $tokenData = $resetTokenModel->verifyToken($token); if (!$tokenData) { Session::setFlash('danger', __('invalid_or_expired_token')); $this->redirect('/forgot-password'); } $this->render('auth/reset_password', [ 'pageTitle' => __('reset_password_title') . ' - CARGOO', 'token' => $token, 'authColClass' => 'auth-container-login' ], 'auth'); } public function resetPassword(): void { $token = $_POST['token'] ?? ''; $newPassword = $_POST['password'] ?? ''; $confirmPassword = $_POST['confirm_password'] ?? ''; $result = $this->authService->resetPassword($token, $newPassword, $confirmPassword); if (!$result['success']) { if (!empty($result['errors'])) { foreach ($result['errors'] as $err) { Session::setFlash('danger', $err); } } else { Session::setFlash('danger', $result['message']); } $this->redirect('/reset-password?token=' . urlencode($token)); } Session::setFlash('success', $result['message']); $this->redirect('/login'); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings