feat: update session + ps type overriding
This commit is contained in:
@ -8,7 +8,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApiService struct {
|
type ApiService struct {
|
||||||
@ -27,21 +26,21 @@ type ApiService struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ServiceSession struct {
|
type ServiceSession struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
ServiceID uuid.UUID `json:"service_id"`
|
ServiceID uuid.UUID `json:"service_id"`
|
||||||
ClientID string `json:"client_id"`
|
ClientID string `json:"client_id"`
|
||||||
UserID *uuid.UUID `json:"user_id"`
|
UserID *uuid.UUID `json:"user_id"`
|
||||||
IssuedAt time.Time `json:"issued_at"`
|
IssuedAt time.Time `json:"issued_at"`
|
||||||
ExpiresAt *time.Time `json:"expires_at"`
|
ExpiresAt *time.Time `json:"expires_at"`
|
||||||
LastActive *time.Time `json:"last_active"`
|
LastActive *time.Time `json:"last_active"`
|
||||||
IpAddress pgtype.Text `json:"ip_address"`
|
IpAddress *string `json:"ip_address"`
|
||||||
UserAgent *string `json:"user_agent"`
|
UserAgent *string `json:"user_agent"`
|
||||||
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
||||||
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
||||||
IsActive bool `json:"is_active"`
|
IsActive bool `json:"is_active"`
|
||||||
RevokedAt *time.Time `json:"revoked_at"`
|
RevokedAt *time.Time `json:"revoked_at"`
|
||||||
Scope *string `json:"scope"`
|
Scope *string `json:"scope"`
|
||||||
Claims []byte `json:"claims"`
|
Claims []byte `json:"claims"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@ -62,17 +61,17 @@ type User struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UserSession struct {
|
type UserSession struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
UserID uuid.UUID `json:"user_id"`
|
UserID uuid.UUID `json:"user_id"`
|
||||||
SessionType string `json:"session_type"`
|
SessionType string `json:"session_type"`
|
||||||
IssuedAt time.Time `json:"issued_at"`
|
IssuedAt time.Time `json:"issued_at"`
|
||||||
ExpiresAt *time.Time `json:"expires_at"`
|
ExpiresAt *time.Time `json:"expires_at"`
|
||||||
LastActive *time.Time `json:"last_active"`
|
LastActive *time.Time `json:"last_active"`
|
||||||
IpAddress pgtype.Text `json:"ip_address"`
|
IpAddress *string `json:"ip_address"`
|
||||||
UserAgent *string `json:"user_agent"`
|
UserAgent *string `json:"user_agent"`
|
||||||
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
||||||
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
||||||
DeviceInfo []byte `json:"device_info"`
|
DeviceInfo []byte `json:"device_info"`
|
||||||
IsActive bool `json:"is_active"`
|
IsActive bool `json:"is_active"`
|
||||||
RevokedAt *time.Time `json:"revoked_at"`
|
RevokedAt *time.Time `json:"revoked_at"`
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const createServiceSession = `-- name: CreateServiceSession :one
|
const createServiceSession = `-- name: CreateServiceSession :one
|
||||||
@ -27,15 +26,15 @@ RETURNING id, service_id, client_id, user_id, issued_at, expires_at, last_active
|
|||||||
`
|
`
|
||||||
|
|
||||||
type CreateServiceSessionParams struct {
|
type CreateServiceSessionParams struct {
|
||||||
ServiceID uuid.UUID `json:"service_id"`
|
ServiceID uuid.UUID `json:"service_id"`
|
||||||
ClientID string `json:"client_id"`
|
ClientID string `json:"client_id"`
|
||||||
UserID *uuid.UUID `json:"user_id"`
|
UserID *uuid.UUID `json:"user_id"`
|
||||||
ExpiresAt *time.Time `json:"expires_at"`
|
ExpiresAt *time.Time `json:"expires_at"`
|
||||||
LastActive *time.Time `json:"last_active"`
|
LastActive *time.Time `json:"last_active"`
|
||||||
IpAddress pgtype.Text `json:"ip_address"`
|
IpAddress *string `json:"ip_address"`
|
||||||
UserAgent *string `json:"user_agent"`
|
UserAgent *string `json:"user_agent"`
|
||||||
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
||||||
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateServiceSession(ctx context.Context, arg CreateServiceSessionParams) (ServiceSession, error) {
|
func (q *Queries) CreateServiceSession(ctx context.Context, arg CreateServiceSessionParams) (ServiceSession, error) {
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const createUserSession = `-- name: CreateUserSession :one
|
const createUserSession = `-- name: CreateUserSession :one
|
||||||
@ -27,15 +26,15 @@ RETURNING id, user_id, session_type, issued_at, expires_at, last_active, ip_addr
|
|||||||
`
|
`
|
||||||
|
|
||||||
type CreateUserSessionParams struct {
|
type CreateUserSessionParams struct {
|
||||||
UserID uuid.UUID `json:"user_id"`
|
UserID uuid.UUID `json:"user_id"`
|
||||||
SessionType string `json:"session_type"`
|
SessionType string `json:"session_type"`
|
||||||
ExpiresAt *time.Time `json:"expires_at"`
|
ExpiresAt *time.Time `json:"expires_at"`
|
||||||
LastActive *time.Time `json:"last_active"`
|
LastActive *time.Time `json:"last_active"`
|
||||||
IpAddress pgtype.Text `json:"ip_address"`
|
IpAddress *string `json:"ip_address"`
|
||||||
UserAgent *string `json:"user_agent"`
|
UserAgent *string `json:"user_agent"`
|
||||||
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
||||||
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
||||||
DeviceInfo []byte `json:"device_info"`
|
DeviceInfo []byte `json:"device_info"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateUserSession(ctx context.Context, arg CreateUserSessionParams) (UserSession, error) {
|
func (q *Queries) CreateUserSession(ctx context.Context, arg CreateUserSessionParams) (UserSession, error) {
|
||||||
@ -233,3 +232,27 @@ func (q *Queries) UpdateSessionLastActive(ctx context.Context, id uuid.UUID) err
|
|||||||
_, err := q.db.Exec(ctx, updateSessionLastActive, id)
|
_, err := q.db.Exec(ctx, updateSessionLastActive, id)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updateSessionTokens = `-- name: UpdateSessionTokens :exec
|
||||||
|
UPDATE user_sessions
|
||||||
|
SET access_token_id = $2, refresh_token_id = $3, expires_at = $4
|
||||||
|
WHERE id = $1
|
||||||
|
AND is_active = TRUE
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateSessionTokensParams struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
AccessTokenID *uuid.UUID `json:"access_token_id"`
|
||||||
|
RefreshTokenID *uuid.UUID `json:"refresh_token_id"`
|
||||||
|
ExpiresAt *time.Time `json:"expires_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateSessionTokens(ctx context.Context, arg UpdateSessionTokensParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, updateSessionTokens,
|
||||||
|
arg.ID,
|
||||||
|
arg.AccessTokenID,
|
||||||
|
arg.RefreshTokenID,
|
||||||
|
arg.ExpiresAt,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
@ -39,6 +39,12 @@ SET last_active = NOW()
|
|||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
AND is_active = TRUE;
|
AND is_active = TRUE;
|
||||||
|
|
||||||
|
-- name: UpdateSessionTokens :exec
|
||||||
|
UPDATE user_sessions
|
||||||
|
SET access_token_id = $2, refresh_token_id = $3, expires_at = $4
|
||||||
|
WHERE id = $1
|
||||||
|
AND is_active = TRUE;
|
||||||
|
|
||||||
-- name: ListAllSessions :many
|
-- name: ListAllSessions :many
|
||||||
SELECT * FROM user_sessions
|
SELECT * FROM user_sessions
|
||||||
ORDER BY issued_at DESC
|
ORDER BY issued_at DESC
|
||||||
|
25
sqlc.yaml
25
sqlc.yaml
@ -41,20 +41,39 @@ sql:
|
|||||||
# ───── text ──────────────────────────────────────────
|
# ───── text ──────────────────────────────────────────
|
||||||
- db_type: "pg_catalog.text"
|
- db_type: "pg_catalog.text"
|
||||||
go_type: { type: "string" }
|
go_type: { type: "string" }
|
||||||
- db_type: "text" # or just "bool"
|
|
||||||
|
- db_type: "text"
|
||||||
go_type: { type: "string" }
|
go_type: { type: "string" }
|
||||||
|
|
||||||
- db_type: "pg_catalog.text"
|
- db_type: "pg_catalog.text"
|
||||||
nullable: true
|
nullable: true
|
||||||
go_type:
|
go_type:
|
||||||
type: "string"
|
type: "string"
|
||||||
pointer: true # ⇒ *bool for NULLable columns
|
pointer: true
|
||||||
|
|
||||||
- db_type: "text"
|
- db_type: "text"
|
||||||
nullable: true
|
nullable: true
|
||||||
go_type:
|
go_type:
|
||||||
type: "string"
|
type: "string"
|
||||||
pointer: true # ⇒ *bool for NULLable columns
|
pointer: true
|
||||||
|
|
||||||
|
- db_type: "pg_catalog.varchar"
|
||||||
|
go_type: { type: "string" }
|
||||||
|
|
||||||
|
- db_type: "varchar"
|
||||||
|
go_type: { type: "string" }
|
||||||
|
|
||||||
|
- db_type: "pg_catalog.varchar"
|
||||||
|
nullable: true
|
||||||
|
go_type:
|
||||||
|
type: "string"
|
||||||
|
pointer: true
|
||||||
|
|
||||||
|
- db_type: "varchar"
|
||||||
|
nullable: true
|
||||||
|
go_type:
|
||||||
|
type: "string"
|
||||||
|
pointer: true
|
||||||
|
|
||||||
# ───── timestamp (WITHOUT TZ) ────────────────────────
|
# ───── timestamp (WITHOUT TZ) ────────────────────────
|
||||||
- db_type: "pg_catalog.timestamp" # or "timestamp"
|
- db_type: "pg_catalog.timestamp" # or "timestamp"
|
||||||
|
Reference in New Issue
Block a user