Update profile page with close all sessions functionality and display active sessions

This commit is contained in:
Luis Eduardo Jeréz Girón
2024-07-23 19:15:31 -06:00
parent eda331085a
commit 19cdeb204c
2 changed files with 46 additions and 4 deletions
@@ -2,13 +2,15 @@ package profile
import (
lucide "github.com/eduardolat/gomponents-lucide"
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/eduardolat/pgbackweb/internal/util/timeutil"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/htmx"
"github.com/maragudk/gomponents"
"github.com/maragudk/gomponents/html"
)
func closeAllSessionsForm() gomponents.Node {
func closeAllSessionsForm(sessions []dbgen.Session) gomponents.Node {
return component.CardBox(component.CardBoxParams{
Children: []gomponents.Node{
component.H2Text("Close all sessions"),
@@ -21,6 +23,35 @@ func closeAllSessionsForm() gomponents.Node {
component.SpanText("Close all sessions"),
lucide.LogOut(),
),
html.Div(html.Class("divider")),
component.H2Text("Active sessions"),
component.PText("All sessions are open for a maximum of 12 hours."),
html.Div(
html.Class("overflow-x-auto"),
html.Table(
html.Class("table"),
html.THead(
html.Tr(
html.Th(component.SpanText("Login time")),
html.Th(component.SpanText("IP address")),
html.Th(component.SpanText("User agent")),
),
),
html.TBody(
component.GMap(sessions, func(session dbgen.Session) gomponents.Node {
return html.Tr(
html.Td(component.SpanText(
session.CreatedAt.Format(timeutil.LayoutYYYYMMDDHHMMSSPretty),
)),
html.Td(component.SpanText(session.Ip)),
html.Td(component.SpanText(session.UserAgent)),
)
}),
),
),
),
},
})
}
+14 -3
View File
@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/eduardolat/pgbackweb/internal/logger"
"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
@@ -14,18 +15,28 @@ import (
)
func (h *handlers) indexPageHandler(c echo.Context) error {
ctx := c.Request().Context()
reqCtx := reqctx.GetCtx(c)
return echoutil.RenderGomponent(c, http.StatusOK, indexPage(reqCtx.User))
sessions, err := h.servs.AuthService.GetUserSessions(ctx, reqCtx.User.ID)
if err != nil {
logger.Error("failed to get user sessions", logger.KV{"err": err})
return c.String(http.StatusInternalServerError, "failed to get user sessions")
}
return echoutil.RenderGomponent(
c, http.StatusOK, indexPage(reqCtx.User, sessions),
)
}
func indexPage(user dbgen.User) gomponents.Node {
func indexPage(user dbgen.User, sessions []dbgen.Session) gomponents.Node {
content := []gomponents.Node{
component.H1Text("Profile"),
html.Div(
html.Class("mt-4 grid grid-cols-2 gap-4"),
html.Div(updateUserForm(user)),
html.Div(closeAllSessionsForm()),
html.Div(closeAllSessionsForm(sessions)),
),
}