File "AuthController.php"

Full Path: C:/wamp64/www/AVIDOTAPP/controllers/AuthController.php
File size: 2.62 KB
MIME-type: text/x-php
Charset: utf-8

<?php
class AuthController {
    public function index() {
        include 'views/auth/login.php';
    }

    public function login() {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            // ── Verificación reCAPTCHA v2 ──────────────────────────────────────
            $recaptchaSecret   = '6LcUafsrAAAAAL2xMNSvimYvzrMlC3YFSgUJGQPx'; // <-- tu secret key
            $recaptchaResponse = $_POST['g-recaptcha-response'] ?? '';

            if (empty($recaptchaResponse)) {
                echo "<script>alert('Por favor completa el reCAPTCHA.'); window.location.href='index.php';</script>";
                return;
            }

            $verifyUrl = 'https://www.google.com/recaptcha/api/siteverify';
            $data = [
                'secret'   => $recaptchaSecret,
                'response' => $recaptchaResponse,
                'remoteip' => $_SERVER['REMOTE_ADDR']
            ];

            $options = [
                'http' => [
                    'method'  => 'POST',
                    'header'  => 'Content-Type: application/x-www-form-urlencoded',
                    'content' => http_build_query($data)
                ]
            ];

            $context        = stream_context_create($options);
            $result         = file_get_contents($verifyUrl, false, $context);
            $recaptchaCheck = json_decode($result, true);

            if (!$recaptchaCheck['success']) {
                echo "<script>alert('reCAPTCHA inválido. Intenta de nuevo.'); window.location.href='index.php';</script>";
                return;
            }
            // ──────────────────────────────────────────────────────────────────

            // Validación de usuario y contraseña
            $user = trim($_POST['user']);
            $pass = trim($_POST['pass']);

            $userModel    = new UserModel();
            $datosUsuario = $userModel->login($user, $pass);

            if ($datosUsuario) {
                $_SESSION['DIGITA'] = $user;
                $_SESSION['GRADO']  = $datosUsuario['GRADO'];

                header("Location: index.php?controller=Dashboard&action=index");
            } else {
                echo "<script>alert('Datos incorrectos'); window.location.href='index.php';</script>";
            }
        }
    }

    public function logout() {
        session_destroy();
        header("Location: index.php");
    }
}
?>