<?php
    // required headers
    header("Access-Control-Allow-Origin: https://eden.imt-lille-douai.fr/~liang.wu/API_LOGIN/identite/");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Max-Age: 3600");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
    
    // files needed to connect to database
    include_once '../config/database.php';
    include_once '../objects/identite.php';
    
    // get database connection
    $database = new Database();
    $db = $database->getConnection();
    
    // instantiate user object
    $identite = new Identite($db);
    $login_exists = $identite->loginExists();
    // get posted data
    $data = json_decode(file_get_contents("php://input"));
    
    // set product property values
    $identite->login = $data->login;
    $identite->password = $data->password;
    
    // check if email exists and if password is correct
    if($login_exists){
            http_response_code(200);
            echo json_encode(array("message" => "Successful login."));}
    
    
    // login failed
    else{
        // set response code
        http_response_code(401);
        // tell the user login failed
        echo json_encode(array("message" => "login inexistant."));
        
    }
    ?>
