From 38e4f5cbecceef02cef7660938d8f155e9725779 Mon Sep 17 00:00:00 2001 From: 0xCA Date: Fri, 29 Dec 2023 15:01:09 +0500 Subject: [PATCH] GetCookiePath util function --- handler/routes.go | 5 +---- handler/session.go | 10 ++-------- router/router.go | 5 +---- util/util.go | 8 ++++++++ 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/handler/routes.go b/handler/routes.go index 781a067..a4f3c07 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -98,10 +98,7 @@ func Login(db store.IStore) echo.HandlerFunc { ageMax = 86400 * 7 } - cookiePath := util.BasePath - if cookiePath == "" { - cookiePath = "/" - } + cookiePath := util.GetCookiePath() sess, _ := session.Get("session", c) sess.Options = &sessions.Options{ diff --git a/handler/session.go b/handler/session.go index 386488f..646774f 100644 --- a/handler/session.go +++ b/handler/session.go @@ -103,10 +103,7 @@ func doRefreshSession(c echo.Context) { return } - cookiePath := util.BasePath - if cookiePath == "" { - cookiePath = "/" - } + cookiePath := util.GetCookiePath() sess.Values["last_update"] = now sess.Options = &sessions.Options{ @@ -217,10 +214,7 @@ func clearSession(c echo.Context) { sess.Options.MaxAge = -1 sess.Save(c.Request(), c.Response()) - cookiePath := util.BasePath - if cookiePath == "" { - cookiePath = "/" - } + cookiePath := util.GetCookiePath() cookie, err := c.Cookie("session_token") if err != nil { diff --git a/router/router.go b/router/router.go index 58e3ec7..59d352e 100644 --- a/router/router.go +++ b/router/router.go @@ -51,10 +51,7 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c func New(tmplDir fs.FS, extraData map[string]interface{}, secret [64]byte) *echo.Echo { e := echo.New() - cookiePath := util.BasePath - if cookiePath == "" { - cookiePath = "/" - } + cookiePath := util.GetCookiePath() cookieStore := sessions.NewCookieStore(secret[:32], secret[32:]) cookieStore.Options.Path = cookiePath diff --git a/util/util.go b/util/util.go index 8655632..d9b9bc0 100644 --- a/util/util.go +++ b/util/util.go @@ -854,3 +854,11 @@ func ConcatMultipleSlices(slices ...[]byte) []byte { return result } + +func GetCookiePath() string { + cookiePath := BasePath + if cookiePath == "" { + cookiePath = "/" + } + return cookiePath +}