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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -161,9 +162,12 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the request with the new context
|
srv.ServeHTTP(w, r.WithContext(ctx))
|
||||||
r = 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 (
|
type (
|
||||||
Session struct {
|
Session struct {
|
||||||
ID gid.GID `db:"id"`
|
ID gid.GID `db:"id"`
|
||||||
UserID gid.GID `db:"user_id"`
|
UserID gid.GID `db:"user_id"`
|
||||||
ExpiredAt time.Time `db:"expired_at"`
|
Data SessionData `db:"data"`
|
||||||
CreatedAt time.Time `db:"created_at"`
|
ExpiredAt time.Time `db:"expired_at"`
|
||||||
UpdatedAt time.Time `db:"updated_at"`
|
CreatedAt time.Time `db:"created_at"`
|
||||||
|
UpdatedAt time.Time `db:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SessionData struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s Session) CursorKey() page.CursorKey {
|
func (s Session) CursorKey() page.CursorKey {
|
||||||
@@ -48,6 +51,7 @@ func (s *Session) LoadByID(
|
|||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
user_id,
|
user_id,
|
||||||
|
data,
|
||||||
expired_at,
|
expired_at,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at
|
updated_at
|
||||||
@@ -80,10 +84,11 @@ func (s *Session) Insert(
|
|||||||
) error {
|
) error {
|
||||||
q := `
|
q := `
|
||||||
INSERT INTO
|
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 (
|
VALUES (
|
||||||
@session_id,
|
@session_id,
|
||||||
@user_id,
|
@user_id,
|
||||||
|
@data,
|
||||||
@expired_at,
|
@expired_at,
|
||||||
@created_at,
|
@created_at,
|
||||||
@updated_at
|
@updated_at
|
||||||
@@ -93,6 +98,7 @@ VALUES (
|
|||||||
args := pgx.StrictNamedArgs{
|
args := pgx.StrictNamedArgs{
|
||||||
"session_id": s.ID,
|
"session_id": s.ID,
|
||||||
"user_id": s.UserID,
|
"user_id": s.UserID,
|
||||||
|
"data": s.Data,
|
||||||
"expired_at": s.ExpiredAt,
|
"expired_at": s.ExpiredAt,
|
||||||
"created_at": s.CreatedAt,
|
"created_at": s.CreatedAt,
|
||||||
"updated_at": s.UpdatedAt,
|
"updated_at": s.UpdatedAt,
|
||||||
@@ -110,13 +116,15 @@ func (s *Session) Update(
|
|||||||
UPDATE usrmgr_sessions
|
UPDATE usrmgr_sessions
|
||||||
SET
|
SET
|
||||||
expired_at = @expired_at,
|
expired_at = @expired_at,
|
||||||
updated_at = @updated_at
|
updated_at = @updated_at,
|
||||||
|
data = @data
|
||||||
WHERE
|
WHERE
|
||||||
id = @session_id
|
id = @session_id
|
||||||
`
|
`
|
||||||
|
|
||||||
args := pgx.StrictNamedArgs{
|
args := pgx.StrictNamedArgs{
|
||||||
"session_id": s.ID,
|
"session_id": s.ID,
|
||||||
|
"user_id": s.UserID,
|
||||||
"expired_at": s.ExpiredAt,
|
"expired_at": s.ExpiredAt,
|
||||||
"updated_at": s.UpdatedAt,
|
"updated_at": s.UpdatedAt,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -420,3 +420,19 @@ func (s Service) GetUserIDFromContext(ctx context.Context) (gid.GID, error) {
|
|||||||
|
|
||||||
return session.UserID, nil
|
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