Add data to session and floating duration
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1
pkg/usrmgr/coredata/migrations/20250305T171200Z.sql
Normal file
1
pkg/usrmgr/coredata/migrations/20250305T171200Z.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE usrmgr_sessions ADD COLUMN data jsonb NOT NULL DEFAULT '{}';
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user