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 justfile
static/*

View file

@ -5,7 +5,7 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "tsc -b && vite build", "build": "tsc -b && vite build --base '/admin' ",
"lint": "eslint .", "lint": "eslint .",
"preview": "vite preview" "preview": "vite preview"
}, },

View file

@ -36,7 +36,13 @@ const NewAccount = () => {
return false 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( 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("") const [totp_img, setTOTPImg] = useState("")
@ -50,6 +56,12 @@ const NewAccount = () => {
let x = totp_generation.parse(await e.json()) let x = totp_generation.parse(await e.json())
setTOTPImg(x.img) setTOTPImg(x.img)
totp.current = x.key 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,14 +32,21 @@ const Panel = () => {
const load = () => { const load = () => {
setLoading(true) setLoading(true)
fetch(rootstr + "/api/admin/fetch_post", { credentials: "include" }).then(async val => { fetch(rootstr + "/api/admin/fetch_post", { credentials: "include" }).then(async val => {
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))) setPosts((await val.json() as Array<Object>).map(post => POST.parse(post)))
}
}).finally(() => { setLoading(false) }) }).finally(() => { setLoading(false) })
} }
const verify = (check: boolean, post: number) => { 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) { if (e.status == 200) {
setPosts(posts.filter(v => v.id != post)) setPosts(posts.filter(v => v.id != post))
} else
if ((e.status == 401 || e.status == 400) && await e.text() == "missing or malformed JWT") {
window.location.href = "/admin/login"
} else { } else {
alert(e.statusText) 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" "encoding/base64"
"image/png" "image/png"
"log" "log"
"os" "path"
"time" "time"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -211,3 +211,22 @@ func AdminVerify(c *fiber.Ctx) error {
return tx.Commit(ctx) 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.Get("/api/heart", handlers.Add_heart)
app.Post("/api/admin/login", handlers.Admin_Login) app.Post("/api/admin/login", handlers.Admin_Login)
app.Static("/static", "./static/") 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{ app.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{Key: []byte(internal.JWT_SECRET)}, SigningKey: jwtware.SigningKey{Key: []byte(internal.JWT_SECRET)},
TokenLookup: "cookie:token", TokenLookup: "cookie:token",