Implement logout

This commit is contained in:
ekzyis 2024-07-14 12:57:40 +02:00
parent 8d84e29d34
commit 782bfec93a
7 changed files with 107 additions and 6 deletions

View File

@ -32,7 +32,6 @@
a:not(.no-link), a:not(.no-link),
button[hx-get], button[hx-get],
button[hx-post] { button[hx-post] {
text-decoration: underline;
transition: background-color 150ms ease-in, color 150ms ease-in; transition: background-color 150ms ease-in, color 150ms ease-in;
} }
@ -43,6 +42,15 @@
color: var(--background-color); color: var(--background-color);
} }
a:not(.no-link),
button[hx-get] {
text-decoration: underline;
}
button[hx-post] {
border-width: 1px;
}
nav a, nav a,
button[hx-get], button[hx-get],
button[hx-post] { button[hx-post] {
@ -81,8 +89,8 @@
} }
.lightning { .lightning {
background-color: var(--lightning-color); background-color: var(--lightning-color) !important;
color: var(--black); color: var(--black) !important;
} }
.lightning:hover { .lightning:hover {
@ -90,8 +98,8 @@
} }
.nostr { .nostr {
background-color: var(--nostr-color); background-color: var(--nostr-color) !important;
color: var(--white); color: var(--white) !important;
} }
.nostr:hover { .nostr:hover {

View File

@ -186,3 +186,33 @@ func mapAction(action string) string {
return action return action
} }
} }
func HandleLogout(sc context.Context) echo.HandlerFunc {
return func(c echo.Context) error {
var (
db = sc.Db
ctx = c.Request().Context()
cookie *http.Cookie
sessionId string
err error
)
if cookie, err = c.Cookie("session"); err != nil {
// cookie not found
return c.JSON(http.StatusNotFound, "session not found")
}
sessionId = cookie.Value
if _, err = db.ExecContext(ctx,
"DELETE FROM sessions WHERE id = $1", sessionId); err != nil {
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()})
return c.Redirect(http.StatusSeeOther, "/")
// c.Response().Header().Set("HX-Location", "/")
// return c.JSON(http.StatusOK, nil)
}
}

View File

@ -0,0 +1,15 @@
package handler
import (
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
"git.ekzyis.com/ekzyis/delphi.market/server/router/pages"
"git.ekzyis.com/ekzyis/delphi.market/types"
"github.com/labstack/echo/v4"
)
func HandleUser(sc context.Context) echo.HandlerFunc {
return func(c echo.Context) error {
u := c.Get("session").(types.User)
return pages.User(&u).Render(context.RenderContext(sc, c), c.Response().Writer)
}
}

View File

@ -46,6 +46,7 @@ func SessionGuard(sc context.Context) echo.MiddlewareFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
session := c.Get("session") session := c.Get("session")
if session == nil { if session == nil {
// this seems to work for non-interactive and htmx requests
return c.Redirect(http.StatusTemporaryRedirect, "/login") return c.Redirect(http.StatusTemporaryRedirect, "/login")
} }
return next(c) return next(c)

View File

@ -4,7 +4,14 @@ import c "git.ekzyis.com/ekzyis/delphi.market/server/router/context"
templ Nav() { templ Nav() {
<header class="mt-3"> <header class="mt-3">
<nav class="flex flex-row" hx-target="#content" hx-swap="outerHTML" hx-select="#content" hx-push-url="true"> <nav
id="nav"
class="flex flex-row"
hx-target="#content"
hx-swap="outerHTML"
hx-select="#content"
hx-push-url="true"
>
<div> <div>
<button hx-get="/">home</button> <button hx-get="/">home</button>
</div> </div>

View File

@ -0,0 +1,37 @@
package pages
import (
"git.ekzyis.com/ekzyis/delphi.market/server/router/pages/components"
"git.ekzyis.com/ekzyis/delphi.market/types"
"strconv"
"time"
)
templ User(user *types.User) {
<html>
@components.Head()
<body class="container">
@components.Nav()
<div id="content" class="flex flex-col">
@components.Figlet("random", "user")
<div
class="grid grid-cols-2 gap-4 my-3 mx-auto"
hx-target="#content"
hx-swap="outerHTML"
hx-select="#content"
hx-push-url="true"
hx-select-oob="#nav"
>
<div class="font-bold">id</div>
<div>{ strconv.Itoa(user.Id) }</div>
<div class="font-bold">joined</div>
<div>{ user.CreatedAt.Format(time.DateOnly) }</div>
<div class="font-bold">sats</div>
<div>{ strconv.Itoa(int(user.Msats) / 1000) }</div>
<button hx-post="/logout" class="col-span-2">logout</button>
</div>
</div>
@components.Footer()
</body>
</html>
}

View File

@ -22,4 +22,7 @@ func Init(e *echo.Echo, sc Context) {
e.GET("/signup/:method", handler.HandleAuth(sc, "register")) e.GET("/signup/:method", handler.HandleAuth(sc, "register"))
e.GET("/api/lnauth/callback", handler.HandleLnAuthCallback(sc)) e.GET("/api/lnauth/callback", handler.HandleLnAuthCallback(sc))
e.GET("/session", handler.HandleSessionCheck(sc)) e.GET("/session", handler.HandleSessionCheck(sc))
e.GET("/user", handler.HandleUser(sc), middleware.SessionGuard(sc))
e.POST("/logout", handler.HandleLogout(sc), middleware.SessionGuard(sc))
} }