separate logout page + redirect

This commit is contained in:
Simon Martens
2025-05-22 21:33:10 +02:00
parent 36e34d9e7b
commit 2a08e5fec7
6 changed files with 71 additions and 5 deletions

View File

@@ -35,4 +35,5 @@ const (
F_INDEX_GO_BUTTON = "GoButton"
P_LOGIN_NAME = "login"
P_LOGOUT_NAME = "logout"
)

View File

@@ -138,6 +138,10 @@ func (p *LoginPage) POST(engine *templating.Engine, app core.App) HandleFunc {
})
}
return e.Redirect(303, "/reihen")
redirect := "/reihen"
if r := e.Request.URL.Query().Get("redirectTo"); r != "" {
redirect = r
}
return e.Redirect(303, redirect)
}
}

52
pages/logout.go Normal file
View File

@@ -0,0 +1,52 @@
package pages
import (
"net/http"
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router"
)
func init() {
lp := &LogoutPage{
StaticPage: pagemodels.StaticPage{
Name: pagemodels.P_LOGOUT_NAME,
Layout: "blank",
Template: "/logout/",
URL: "/logout/",
},
}
app.Register(lp)
}
type LogoutPage struct {
pagemodels.StaticPage
}
func (p *LogoutPage) Setup(router *router.Router[*core.RequestEvent], app core.App, engine *templating.Engine) error {
router.GET(p.URL, p.GET())
return nil
}
func (p *LogoutPage) GET() HandleFunc {
return func(e *core.RequestEvent) error {
// TODO: the function to delete tokens is not yet there
// as of right now, the tokens get only deleted from the clients
// We need to delete the tokens from the cache + table.
e.SetCookie(&http.Cookie{
Name: dbmodels.SESSION_COOKIE_NAME,
Path: "/",
MaxAge: -1,
})
e.Response.Header().Set("Clear-Site-Data", "\"cookies\"")
redirect := "/reihen"
if r := e.Request.URL.Query().Get("redirectTo"); r != "" {
redirect = r
}
return e.Redirect(303, redirect)
}
}

View File

@@ -342,6 +342,7 @@ func (e *Engine) Response200(request *core.RequestEvent, path string, ld map[str
func requestData(request *core.RequestEvent) map[string]any {
data := make(map[string]any)
data["Path"] = request.Request.URL.Path
data["FullPath"] = GetRequestPathWithQuery(request.Request)
data["Query"] = request.Request.URL.Query()
data["Method"] = request.Request.Method
data["Host"] = request.Request.Host
@@ -362,3 +363,11 @@ func requestData(request *core.RequestEvent) map[string]any {
return data
}
func GetRequestPathWithQuery(r *http.Request) string {
path := r.URL.EscapedPath()
if r.URL.RawQuery != "" {
return path + "?" + r.URL.RawQuery
}
return path
}

View File

@@ -24,10 +24,10 @@
{{ .page.User.Email }}
|
<i class="ri-logout-box-line"></i>
<a href="/login">Logout</a>
<a href="/logout?redirectTo={{ .page.FullPath }}">Logout</a>
{{ else }}
<i class="ri-login-box-line"></i>
<a href="/login">Login</a>
<a href="/login?redirectTo={{ .page.FullPath }}">Login</a>
{{ end }}
</div>
</div>

View File

@@ -12,7 +12,7 @@
</div>
</div>
<h1 class="text-4xl self-baseline text-center w-full mt-6">Musenalm | Login</h1>
<form class="mt-9 w-full grid grid-cols-3 gap-4" method="POST" action="/login/">
<form class="mt-9 w-full grid grid-cols-3 gap-4" method="POST">
<div
class="col-span-3 border-2 border-transparent focus-within:border-slate-600 px-2 py-1 pb-1.5
bg-slate-200 focus-within:bg-slate-50 rounded-md transition-all duration-100">