<?php
    include_once('../components/connect.php');

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: access");
    header("Access-Control-Allow-Methods: POST");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
    include_once('helper_functions.php');

    $resp['msg'] = '';
    $resp['success'] = false;

    $u_email = validate_sql($_POST['user_email']);
    $u_password = validate_sql($_POST['user_password']);
    $u_password = md5($u_password);

    if ($u_email != "" && $u_password != "")
    {
        $sql = "SELECT * FROM users WHERE user_email = '$u_email' AND user_password = '$u_password'";
        $result = mysqli_query($conn, $sql);
        $row = mysqli_fetch_assoc($result);
        if ($row) 
        {
            $_SESSION['user_id'] = $row['user_id'];
            $resp['msg'] = '';
            $resp['success'] = true;
        } else 
        {
            $resp['msg'] = 'Invalid Login Credentials';
            $resp['success'] = false;
        }
        echo json_encode($resp);
    }
    
?>