<?php
include_once("auth.php");
//Require already existing user to use this function 
require "vendor/autoload.php";
$server = new \PHPCouchDB\Server(["url" => "http://localhost:5984"]);

function mv($var){
	if (isset($_GET[$var]) && $_GET[$var] != ""){
		return $_GET[$var];
	}else{
		return false;
	}
}

if (mv("username") != false && mv("pass") != false){
	//Create new user
	$username = trim(strip_tags(mv("username")));
	$password = hash('sha512',strip_tags(mv("pass")));
	$authDB = $server->useDb(["name" => "auth", "create_if_not_exists" => true]);
	$doc = $authDB->create(["username" => $username, "password" => $password , "admin" => false, "registerDate" => time()]);
	header('Content-Type: application/json');
	//Return the preview of the JSON code of the document
	echo json_encode($doc);
}else{
	//Redirect to 404 page. Not implemented yet
	
}
