2023-05-20 20:15:57 +02:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-05-10 19:09:41 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo-contrib/session"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Logout() echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2023-09-08 17:22:20 +02:00
|
|
|
defer addHeaders(c)
|
2023-05-11 23:50:40 +02:00
|
|
|
|
2023-05-10 19:09:41 +02:00
|
|
|
switch {
|
2023-08-24 18:43:24 +02:00
|
|
|
case c.Request().Method == "POST": // nolint:goconst
|
2023-05-10 19:09:41 +02:00
|
|
|
sess, _ := session.Get(setting.SessionCookieName(), c)
|
|
|
|
if sess != nil {
|
|
|
|
log.Infof("max-age before logout: %d", sess.Options.MaxAge)
|
|
|
|
sess.Options.MaxAge = -1
|
|
|
|
|
2023-09-08 17:22:20 +02:00
|
|
|
delete(sess.Values, "username")
|
2023-05-10 19:09:41 +02:00
|
|
|
|
|
|
|
err := sess.Save(c.Request(), c.Response())
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error("could not delete session cookie")
|
2023-09-08 17:22:20 +02:00
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
2023-05-10 19:09:41 +02:00
|
|
|
}
|
2023-09-08 17:22:20 +02:00
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderCacheControl, "no-store")
|
2023-05-10 19:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusMovedPermanently, "/logout")
|
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
case c.Request().Method == "GET": // nolint:goconst
|
2023-08-07 14:21:48 +02:00
|
|
|
sess, _ := session.Get(setting.SessionCookieName(), c)
|
|
|
|
if sess != nil {
|
2023-09-08 17:22:20 +02:00
|
|
|
if uname, ok := sess.Values["username"].(string); ok {
|
|
|
|
if uname != "" {
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/home")
|
|
|
|
}
|
2023-08-07 14:21:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Logout"
|
|
|
|
p.Current = "logout"
|
|
|
|
|
2023-05-10 19:09:41 +02:00
|
|
|
err := c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"logout.tmpl",
|
2023-08-15 18:33:48 +02:00
|
|
|
p,
|
2023-05-10 19:09:41 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Errorf("error: %q", err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|