diff --git a/pagemodels/pagedata.go b/pagemodels/pagedata.go index 0dd8a45..d711efc 100644 --- a/pagemodels/pagedata.go +++ b/pagemodels/pagedata.go @@ -34,5 +34,6 @@ const ( F_INDEX_TEXTE_ABS2 = "Abs2" F_INDEX_GO_BUTTON = "GoButton" - P_LOGIN_NAME = "login" + P_LOGIN_NAME = "login" + P_LOGOUT_NAME = "logout" ) diff --git a/pages/login.go b/pages/login.go index 22cbe4b..842eaee 100644 --- a/pages/login.go +++ b/pages/login.go @@ -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) } } diff --git a/pages/logout.go b/pages/logout.go new file mode 100644 index 0000000..052aa5a --- /dev/null +++ b/pages/logout.go @@ -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) + } +} diff --git a/templating/engine.go b/templating/engine.go index 4e2beca..a7ba112 100644 --- a/templating/engine.go +++ b/templating/engine.go @@ -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 +} diff --git a/views/layouts/components/_footer.gohtml b/views/layouts/components/_footer.gohtml index c0e145e..d854277 100644 --- a/views/layouts/components/_footer.gohtml +++ b/views/layouts/components/_footer.gohtml @@ -24,10 +24,10 @@ {{ .page.User.Email }} | - Logout + Logout {{ else }} - Login + Login {{ end }} diff --git a/views/routes/login/body.gohtml b/views/routes/login/body.gohtml index 063fdef..330046b 100644 --- a/views/routes/login/body.gohtml +++ b/views/routes/login/body.gohtml @@ -12,7 +12,7 @@

Musenalm | Login

-
+