diff --git a/.gitignore b/.gitignore index be29348..cad3232 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ justfile - +static/* diff --git a/admin_panel/package.json b/admin_panel/package.json index 52d3f83..e853c7e 100644 --- a/admin_panel/package.json +++ b/admin_panel/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "dev": "vite", - "build": "tsc -b && vite build", + "build": "tsc -b && vite build --base '/admin' ", "lint": "eslint .", "preview": "vite preview" }, diff --git a/admin_panel/src/NewAccount.tsx b/admin_panel/src/NewAccount.tsx index 37126f7..4fa308c 100644 --- a/admin_panel/src/NewAccount.tsx +++ b/admin_panel/src/NewAccount.tsx @@ -36,7 +36,13 @@ const NewAccount = () => { return false } fetch(rootstr + "/api/admin/create", { credentials: "include", body: JSON.stringify({ "name": data.name, "password": data.password, "totp_secret": totp.current }), headers: { "Content-Type": "application/json" }, method: "POST" }).then( - e => { alert(e.statusText) }).finally(() => SetCreateState(false)) + async e => { + if ((e.status == 401 || e.status == 400) && await e.text() == "missing or malformed JWT") { + window.location.href = "/admin/login" + } else { + alert(e.statusText) + } + }).finally(() => SetCreateState(false)) } const [totp_img, setTOTPImg] = useState("") @@ -50,7 +56,13 @@ const NewAccount = () => { let x = totp_generation.parse(await e.json()) setTOTPImg(x.img) totp.current = x.key - } + } else + if ((e.status == 401 || e.status == 400) && await e.text() == "missing or malformed JWT") { + window.location.href = "/admin/login" + } + else { + alert(e.statusText + " " + e.text()) + } }) } } diff --git a/admin_panel/src/Panel.tsx b/admin_panel/src/Panel.tsx index 4bb2138..7a0afca 100644 --- a/admin_panel/src/Panel.tsx +++ b/admin_panel/src/Panel.tsx @@ -32,17 +32,24 @@ const Panel = () => { const load = () => { setLoading(true) fetch(rootstr + "/api/admin/fetch_post", { credentials: "include" }).then(async val => { - setPosts((await val.json() as Array).map(post => POST.parse(post))) + if ((val.status == 401 || val.status == 400) && await val.text() == "missing or malformvald JWT") { + window.location.href = "/admin/login" + } else if (val.status == 200) { + setPosts((await val.json() as Array).map(post => POST.parse(post))) + } }).finally(() => { setLoading(false) }) } const verify = (check: boolean, post: number) => { - fetch(rootstr + "/api/admin/verify_post", { method: "PUT", credentials: "include", body: JSON.stringify({ "post": post, "check": check, }), headers: { "Content-Type": "application/json" } }).then(e => { + fetch(rootstr + "/api/admin/verify_post", { method: "PUT", credentials: "include", body: JSON.stringify({ "post": post, "check": check, }), headers: { "Content-Type": "application/json" } }).then(async e => { if (e.status == 200) { setPosts(posts.filter(v => v.id != post)) - } else { - alert(e.statusText) - } + } else + if ((e.status == 401 || e.status == 400) && await e.text() == "missing or malformed JWT") { + window.location.href = "/admin/login" + } else { + alert(e.statusText) + } }) diff --git a/admin_panel/src/rooturl.ts b/admin_panel/src/rooturl.ts index d023df1..d96b878 100644 --- a/admin_panel/src/rooturl.ts +++ b/admin_panel/src/rooturl.ts @@ -1 +1,2 @@ -export const rootstr = "http://localhost:3000"; +// export const rootstr = "http://localhost:3000"; +export const rootstr = ""; diff --git a/internal/handlers/admin.go b/internal/handlers/admin.go index abe41fe..7e3e7f5 100644 --- a/internal/handlers/admin.go +++ b/internal/handlers/admin.go @@ -5,7 +5,7 @@ import ( "encoding/base64" "image/png" "log" - "os" + "path" "time" "github.com/gofiber/fiber/v2" @@ -211,3 +211,22 @@ func AdminVerify(c *fiber.Ctx) error { return tx.Commit(ctx) } + +const adminpage_basepath = "./admin_panel/dist" + +func AdminSendPage(c *fiber.Ctx) error { + pagepath := c.Params("*") + if pagepath == "login" || pagepath == "panel" || pagepath == "new_account" { + err := c.SendFile(path.Join(adminpage_basepath, "index.html")) + if err != nil { + log.Println(err) + return c.SendStatus(fiber.StatusNotFound) + } + return nil + } + err := c.SendFile(path.Join(adminpage_basepath, pagepath)) + if err != nil { + c.SendStatus(404) + } + return nil +} diff --git a/server.go b/server.go index 0489c2a..f83434e 100644 --- a/server.go +++ b/server.go @@ -59,6 +59,11 @@ func main() { app.Get("/api/heart", handlers.Add_heart) app.Post("/api/admin/login", handlers.Admin_Login) app.Static("/static", "./static/") + + app.Get("/admin", func(c *fiber.Ctx) error { + return c.Redirect("/admin/login") + }) + app.Get("/admin/*", handlers.AdminSendPage) app.Use(jwtware.New(jwtware.Config{ SigningKey: jwtware.SigningKey{Key: []byte(internal.JWT_SECRET)}, TokenLookup: "cookie:token",