浏览代码

Create the front-end for first admin creation

Lee Morgan 3 周之前
父节点
当前提交
e3ea482c26
共有 3 个文件被更改,包括 23 次插入6 次删除
  1. 4 3
      src/controllers/first_admin.rs
  2. 18 1
      ui/adminCreate/index.js
  3. 1 2
      ui/index.js

+ 4 - 3
src/controllers/first_admin.rs

@@ -2,6 +2,7 @@ use actix_web::{HttpResponse, web, post};
 use validator::ValidateEmail;
 use uuid::Uuid;
 use serde::Deserialize;
+use serde_json::json;
 use std::sync::Mutex;
 use crate::{
     users::{User, UserAccess},
@@ -23,7 +24,7 @@ pub async fn route(
 ) -> HttpResponse {
     let b = match validate_body(body) {
         Ok(b) => b,
-        Err(e) => { return HttpResponse::BadRequest().body(e.to_string())}
+        Err(e) => { return HttpResponse::BadRequest().json(json!({"msg": e.to_string()}))}
     };
 
     let new_user = User {
@@ -43,11 +44,11 @@ pub async fn route(
         User::write(guard.to_vec());
     }
 
-    HttpResponse::Ok().body("success")
+    HttpResponse::Ok().json(json!({"msg": "success"}))
 }
 
 fn validate_body(body: Body) -> Result<Body, Box<dyn std::error::Error>> {
-    if body.email.validate_email() {
+    if !body.email.validate_email() {
         return Err("Invalid email".into());
     }
 

+ 18 - 1
ui/adminCreate/index.js

@@ -21,6 +21,23 @@ export default {
             password: document.getElementById("adminCreatePass").value,
             confirm_password: document.getElementById("adminCreatePassConf").value
         };
-        console.log(data);
+
+        fetch("/admin/first", {
+            method: "POST",
+            headers: {"Content-Type": "application/json"},
+            credentials: "include",
+            body: JSON.stringify(data)
+        })
+            .then((r)=>{
+                if(!r.ok) return console.log(r.msg);
+
+                return r.json();
+            })
+            .then((response)=>{
+                openPage("login");
+            })
+            .catch((e)=>{
+                console.log(e);
+            });
     }
 }

+ 1 - 2
ui/index.js

@@ -7,7 +7,7 @@ window.adminExists = "true";
 let pages = document.querySelectorAll(".page");
 
 //Page change function
-let openPage = (page)=>{
+openPage = (page)=>{
     for(let i = 0; i < pages.length; i++){
         pages[i].style.display = "none";
     }
@@ -22,7 +22,6 @@ let openPage = (page)=>{
 }
 
 //Check for login
-console.log(window.adminExists);
 if(window.adminExists !== "true"){
     openPage("adminCreate");
 }else if(localStorage.getItem("loggedIn") === "true"){