auto_redirect and root path adjust and admin panel existance
This commit is contained in:
parent
0795e3ff61
commit
596118b535
7 changed files with 55 additions and 11 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,2 @@
|
|||
justfile
|
||||
|
||||
static/*
|
||||
|
|
|
@ -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"
|
||||
},
|
||||
|
|
|
@ -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,6 +56,12 @@ 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())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -32,14 +32,21 @@ const Panel = () => {
|
|||
const load = () => {
|
||||
setLoading(true)
|
||||
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)))
|
||||
}
|
||||
}).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
|
||||
if ((e.status == 401 || e.status == 400) && await e.text() == "missing or malformed JWT") {
|
||||
window.location.href = "/admin/login"
|
||||
} else {
|
||||
alert(e.statusText)
|
||||
}
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
export const rootstr = "http://localhost:3000";
|
||||
// export const rootstr = "http://localhost:3000";
|
||||
export const rootstr = "";
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Add table
Reference in a new issue