diff --git a/pkg/api/console/v1/resolver.go b/pkg/api/console/v1/resolver.go index 0c64161f8..2cc8b6e8f 100644 --- a/pkg/api/console/v1/resolver.go +++ b/pkg/api/console/v1/resolver.go @@ -18,6 +18,7 @@ package console_v1 import ( "context" + "fmt" "net/http" "time" @@ -161,9 +162,12 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg } } - // Update the request with the new context - r = r.WithContext(ctx) + srv.ServeHTTP(w, r.WithContext(ctx)) - srv.ServeHTTP(w, r) + if session := SessionFromContext(r.Context()); session != nil { + if err := usrmgrSvc.UpdateSession(r.Context(), session); err != nil { + panic(fmt.Errorf("failed to update session: %w", err)) + } + } } } diff --git a/pkg/usrmgr/coredata/migrations/20250305T171200Z.sql b/pkg/usrmgr/coredata/migrations/20250305T171200Z.sql new file mode 100644 index 000000000..eddd99aaa --- /dev/null +++ b/pkg/usrmgr/coredata/migrations/20250305T171200Z.sql @@ -0,0 +1 @@ +ALTER TABLE usrmgr_sessions ADD COLUMN data jsonb NOT NULL DEFAULT '{}'; diff --git a/pkg/usrmgr/coredata/session.go b/pkg/usrmgr/coredata/session.go index 92d75d56a..9524af732 100644 --- a/pkg/usrmgr/coredata/session.go +++ b/pkg/usrmgr/coredata/session.go @@ -27,12 +27,15 @@ import ( type ( Session struct { - ID gid.GID `db:"id"` - UserID gid.GID `db:"user_id"` - ExpiredAt time.Time `db:"expired_at"` - CreatedAt time.Time `db:"created_at"` - UpdatedAt time.Time `db:"updated_at"` + ID gid.GID `db:"id"` + UserID gid.GID `db:"user_id"` + Data SessionData `db:"data"` + ExpiredAt time.Time `db:"expired_at"` + CreatedAt time.Time `db:"created_at"` + UpdatedAt time.Time `db:"updated_at"` } + + SessionData struct{} ) func (s Session) CursorKey() page.CursorKey { @@ -48,6 +51,7 @@ func (s *Session) LoadByID( SELECT id, user_id, + data, expired_at, created_at, updated_at @@ -80,10 +84,11 @@ func (s *Session) Insert( ) error { q := ` INSERT INTO - usrmgr_sessions (id, user_id, expired_at, created_at, updated_at) + usrmgr_sessions (id, user_id, data, expired_at, created_at, updated_at) VALUES ( @session_id, @user_id, + @data, @expired_at, @created_at, @updated_at @@ -93,6 +98,7 @@ VALUES ( args := pgx.StrictNamedArgs{ "session_id": s.ID, "user_id": s.UserID, + "data": s.Data, "expired_at": s.ExpiredAt, "created_at": s.CreatedAt, "updated_at": s.UpdatedAt, @@ -110,13 +116,15 @@ func (s *Session) Update( UPDATE usrmgr_sessions SET expired_at = @expired_at, - updated_at = @updated_at + updated_at = @updated_at, + data = @data WHERE id = @session_id ` args := pgx.StrictNamedArgs{ "session_id": s.ID, + "user_id": s.UserID, "expired_at": s.ExpiredAt, "updated_at": s.UpdatedAt, } diff --git a/pkg/usrmgr/usrmgr.go b/pkg/usrmgr/usrmgr.go index b7b8e5f1a..58b059c7c 100644 --- a/pkg/usrmgr/usrmgr.go +++ b/pkg/usrmgr/usrmgr.go @@ -420,3 +420,19 @@ func (s Service) GetUserIDFromContext(ctx context.Context) (gid.GID, error) { return session.UserID, nil } + +// UpdateSession updates a session in the database +func (s Service) UpdateSession( + ctx context.Context, + session *coredata.Session, +) error { + session.UpdatedAt = time.Now() + session.ExpiredAt = time.Now().Add(24 * time.Hour) + + return s.pg.WithTx( + ctx, + func(tx pg.Conn) error { + return session.Update(ctx, tx) + }, + ) +}