<?php 

require_once '../includes/DbOperations.php';

$response = array();

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

	if(isset($_POST['username']) and isset($_POST['password'])){
		$db = new DbOperations();

		$username = $_POST['username'];
		$password = $_POST['password'];

		if($db -> userLogin($_POST['username'], $_POST['password']) ){
			$user = $db->getUserByUsername($username);
			$response['error'] = false;
			$response['id'] = $user['id'];
			$response['email'] = $user['email'];
			$response['username'] = $user['username'];
			$response['role'] = $user['role'];

		}else{
			$response['error'] = true;
			$response['message'] = "Invalid username or password entered";	
		}
	}else{

			$response['error'] = true;
			$response['message'] = "Required fields are missing";	
	}
}
header('Content-Type: application/json');
echo json_encode($response);
 