auto_redirect and root path adjust and admin panel existance

This commit is contained in:
jasinco 2025-06-01 23:01:13 +08:00
parent 0795e3ff61
commit 596118b535
7 changed files with 55 additions and 11 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
justfile
static/*

View file

@ -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"
},

View file

@ -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())
}
})
}
}

View file

@ -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<Object>).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<Object>).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)
}
})

View file

@ -1 +1,2 @@
export const rootstr = "http://localhost:3000";
// export const rootstr = "http://localhost:3000";
export const rootstr = "";

View file

@ -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
}

View file

@ -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",