<?php
include_once("database.php");
$postData = file_get_contents("php://input");

if (isset($postData) && !empty($postData)) {
    $request = json_decode($postData);
    $name = trim($request->name);
    $pwd = password_hash(mysqli_real_escape_string($mysqli, trim($request->pwd)), PASSWORD_DEFAULT);
    $email = mysqli_real_escape_string($mysqli, trim($request->email));
    $username = mysqli_real_escape_string($mysqli, trim($request->username));
    $sql = "INSERT INTO users(name,password,email,username) VALUES ('$name','$pwd','$email','$username')";

    if ($mysqli->query($sql) === TRUE) {
        $authData = [
            'name' => $name,
            'pwd' => $pwd,
            'email' => $email,
            'id' => mysqli_insert_id($mysqli),
            'username' => $username
        ];

        mkdir('../users/' . $authData['username']);

        echo json_encode($authData);
    }

    else {
        echo "mysqli_error($mysqli)";
    }
}
