<?php
class Login extends controller
{
    public function index($arg = "")
    {

        if (isset($_POST['user']) && isset($_POST['password'])) {
            $email = $_POST['user'];
            $password = $_POST['password'];
            try {
                $obj = $this->DB->dbFetchObject("SELECT * FROM users WHERE email= :email", ['email' => $email]);

                if (($obj && $obj->user_name) && (password_verify($password, $obj->user_pwd))) {
                    $token = $this->createJWT($obj);
                    echo json_encode(['message' => "OK", 'status' => 200, 'token' => $token]);
                } else {
                    echo json_encode([
                        'status' => 500,
                        'message' => 'Invalid User or Password'
                    ]);
                }
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
    }
}
