redirect on /admin, push tweaks
This commit is contained in:
parent
c75826e4c1
commit
55df303d2b
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,3 +3,5 @@ puerta.db
|
|||||||
local.db
|
local.db
|
||||||
config.joao.yaml
|
config.joao.yaml
|
||||||
test.db
|
test.db
|
||||||
|
puerta.db-shm
|
||||||
|
puerta.db-wal
|
||||||
|
@ -82,6 +82,17 @@ func RequireAuthOrRedirect(handler httprouter.Handle, target string) httprouter.
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RequireAdminOrRedirect(handler httprouter.Handle, target string) httprouter.Handle {
|
||||||
|
return withUser(func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
|
if req.Context().Value(constants.ContextUser) == nil {
|
||||||
|
http.Redirect(w, req, target, http.StatusTemporaryRedirect)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
handler(w, req, ps)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func RegisterSecondFactor() httprouter.Handle {
|
func RegisterSecondFactor() httprouter.Handle {
|
||||||
return RequireAuth(func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
return RequireAuth(func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
u := user.FromContext(req)
|
u := user.FromContext(req)
|
||||||
|
@ -256,7 +256,7 @@ func Initialize(config *Config) (http.Handler, error) {
|
|||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
w.Write(buf)
|
w.Write(buf)
|
||||||
})
|
})
|
||||||
router.GET("/admin", auth.RequireAdmin(renderTemplate(bytes.ReplaceAll(adminTemplate, []byte("$PUSH_KEY$"), []byte(config.WebPush.Key.Public)))))
|
router.GET("/admin", auth.RequireAdminOrRedirect(renderTemplate(bytes.ReplaceAll(adminTemplate, []byte("$PUSH_KEY$"), []byte(config.WebPush.Key.Public))), "/login?next=/admin"))
|
||||||
|
|
||||||
// regular api
|
// regular api
|
||||||
router.POST("/api/login", auth.LoginHandler)
|
router.POST("/api/login", auth.LoginHandler)
|
||||||
|
@ -296,20 +296,37 @@ window.addEventListener("load", async function() {
|
|||||||
scope: "/"
|
scope: "/"
|
||||||
})
|
})
|
||||||
|
|
||||||
const sub = await reg.pushManager.getSubscription()
|
try {
|
||||||
console.log(`registered SW, push sub: ${sub}`, reg)
|
sub = await reg.pushManager.getSubscription()
|
||||||
|
if (sub) {
|
||||||
|
pnb.classList.add("subscribed")
|
||||||
|
pnb.innerHTML = "🔕"
|
||||||
|
} else {
|
||||||
|
pnb.classList.remove("subscribed")
|
||||||
|
pnb.innerHTML = "🔔"
|
||||||
|
}
|
||||||
|
} catch(err) {
|
||||||
|
console.error("Could not get pushmanager subscription", err)
|
||||||
|
}
|
||||||
|
|
||||||
const pnb = document.querySelector("#push-notifications")
|
const pnb = document.querySelector("#push-notifications")
|
||||||
|
|
||||||
if (sub) {
|
|
||||||
pnb.classList.add("subscribed")
|
|
||||||
pnb.innerHTML = "🔕"
|
|
||||||
} else {
|
|
||||||
pnb.classList.remove("subscribed")
|
|
||||||
pnb.innerHTML = "🔔"
|
|
||||||
}
|
|
||||||
|
|
||||||
pnb.addEventListener('click', async evt =>{
|
pnb.addEventListener('click', async evt =>{
|
||||||
|
let sub
|
||||||
|
try {
|
||||||
|
sub = await reg.pushManager.getSubscription()
|
||||||
|
} catch(err) {
|
||||||
|
console.error("Could not get pushmanager subscription", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sub) {
|
||||||
|
pnb.classList.add("subscribed")
|
||||||
|
pnb.innerHTML = "🔕"
|
||||||
|
} else {
|
||||||
|
pnb.classList.remove("subscribed")
|
||||||
|
pnb.innerHTML = "🔔"
|
||||||
|
}
|
||||||
|
|
||||||
if (!pnb.classList.contains("subscribed")) {
|
if (!pnb.classList.contains("subscribed")) {
|
||||||
if (await createPushSubscription()) {
|
if (await createPushSubscription()) {
|
||||||
pnb.classList.add("subscribed")
|
pnb.classList.add("subscribed")
|
||||||
|
@ -34,7 +34,17 @@ function submit(evt){
|
|||||||
clearStatus()
|
clearStatus()
|
||||||
|
|
||||||
Login().then(() => {
|
Login().then(() => {
|
||||||
window.location = "/";
|
let next = "/"
|
||||||
|
try {
|
||||||
|
const follow = window.location.search.replace("?next=", "")
|
||||||
|
if (follow != "") {
|
||||||
|
next = follow
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Could not find next path to follow: ${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
window.location = next;
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
form.classList.add("failed")
|
form.classList.add("failed")
|
||||||
document.querySelector('.error').innerText = err
|
document.querySelector('.error').innerText = err
|
||||||
|
Loading…
Reference in New Issue
Block a user