2023-09-10 15:39:34 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.ekzyis.com/ekzyis/delphi.market/db"
|
2023-10-04 11:36:54 +00:00
|
|
|
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
2023-09-10 15:39:34 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2024-07-09 20:41:29 +00:00
|
|
|
func HandleLogout(sc context.Context) echo.HandlerFunc {
|
2023-09-10 15:39:34 +00:00
|
|
|
return func(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
cookie *http.Cookie
|
|
|
|
sessionId string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if cookie, err = c.Cookie("session"); err != nil {
|
|
|
|
// cookie not found
|
2023-11-07 12:51:33 +00:00
|
|
|
return c.JSON(http.StatusNotFound, map[string]string{"reason": "session not found"})
|
2023-09-10 15:39:34 +00:00
|
|
|
}
|
|
|
|
sessionId = cookie.Value
|
2023-10-04 11:36:54 +00:00
|
|
|
if err = sc.Db.DeleteSession(&db.Session{SessionId: sessionId}); err != nil {
|
2023-09-10 15:39:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// tell browser that cookie is expired and thus can be deleted
|
|
|
|
c.SetCookie(&http.Cookie{Name: "session", HttpOnly: true, Path: "/", Value: sessionId, Secure: true, Expires: time.Now()})
|
2023-11-07 12:51:33 +00:00
|
|
|
return c.JSON(http.StatusSeeOther, map[string]string{"status": "OK"})
|
2023-09-10 15:39:34 +00:00
|
|
|
}
|
|
|
|
}
|