summaryrefslogtreecommitdiff
path: root/static/register.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/register.js')
-rw-r--r--static/register.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/static/register.js b/static/register.js
new file mode 100644
index 0000000..09175d6
--- /dev/null
+++ b/static/register.js
@@ -0,0 +1,50 @@
+
+function display_error(message) {
+ document.getElementById("error-message").innerHTML = `Error: ${message}`;
+ document.getElementById("error").hidden = false;
+}
+
+function init() {
+ const form = document.getElementById("register-form");
+ form.addEventListener("submit", async (e) => {
+ e.preventDefault();
+
+ const body = {}
+ new FormData(form).forEach((value, key) => body[key] = value);
+
+ try {
+ console.log();
+
+ const reg_res = await fetch("/user", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify(body)
+ });
+
+ if (!reg_res.ok) {
+ const error = await reg_res.json();
+ display_error(error.error);
+ return;
+ }
+
+ const log_res = await fetch("/login", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ credentials: "include",
+ body: JSON.stringify(body)
+ });
+
+ if (!log_res.ok) {
+ const error = await log_res.json();
+ display_error(error.error);
+ return;
+ }
+
+ window.location.href = "index.html"
+
+ } catch (err) {
+ console.log(err);
+ display_error("network error");
+ }
+ });
+}