Fix lastSession always return an error
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -348,15 +348,15 @@ WHERE
|
|||||||
|
|
||||||
rows, err := conn.Query(ctx, q, args)
|
rows, err := conn.Query(ctx, q, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == pgx.ErrNoRows {
|
|
||||||
return ErrResourceNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("cannot query session: %w", err)
|
return fmt.Errorf("cannot query session: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Session])
|
session, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Session])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == pgx.ErrNoRows {
|
||||||
|
return ErrResourceNotFound
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Errorf("cannot collect session: %w", err)
|
return fmt.Errorf("cannot collect session: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -304,14 +304,6 @@ func (s SessionService) GetActiveSessionForMembership(ctx context.Context, rootS
|
|||||||
return fmt.Errorf("cannot load child session: %w", err)
|
return fmt.Errorf("cannot load child session: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if childSession.ExpireReason != nil {
|
|
||||||
return NewSessionExpiredError(childSession.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
if time.Now().After(childSession.ExpiredAt) {
|
|
||||||
return NewSessionExpiredError(childSession.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -81,6 +81,10 @@ func IsViewerDirective(ctx context.Context, obj any, next graphql.Resolver) (any
|
|||||||
if identity.ID != node.IdentityID {
|
if identity.ID != node.IdentityID {
|
||||||
return nil, ErrForbidden
|
return nil, ErrForbidden
|
||||||
}
|
}
|
||||||
|
case *types.Session:
|
||||||
|
if identity.ID != node.IdentityID {
|
||||||
|
return nil, ErrForbidden
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -268,6 +268,7 @@ type Invitation implements Node {
|
|||||||
|
|
||||||
type Session implements Node {
|
type Session implements Node {
|
||||||
id: ID!
|
id: ID!
|
||||||
|
identityId: ID!
|
||||||
ipAddress: String!
|
ipAddress: String!
|
||||||
userAgent: String!
|
userAgent: String!
|
||||||
updatedAt: Datetime!
|
updatedAt: Datetime!
|
||||||
|
|||||||
@@ -387,12 +387,13 @@ type ComplexityRoot struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Session struct {
|
Session struct {
|
||||||
CreatedAt func(childComplexity int) int
|
CreatedAt func(childComplexity int) int
|
||||||
ExpiresAt func(childComplexity int) int
|
ExpiresAt func(childComplexity int) int
|
||||||
ID func(childComplexity int) int
|
ID func(childComplexity int) int
|
||||||
IPAddress func(childComplexity int) int
|
IPAddress func(childComplexity int) int
|
||||||
UpdatedAt func(childComplexity int) int
|
IdentityID func(childComplexity int) int
|
||||||
UserAgent func(childComplexity int) int
|
UpdatedAt func(childComplexity int) int
|
||||||
|
UserAgent func(childComplexity int) int
|
||||||
}
|
}
|
||||||
|
|
||||||
SessionConnection struct {
|
SessionConnection struct {
|
||||||
@@ -1880,6 +1881,12 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
return e.complexity.Session.IPAddress(childComplexity), true
|
return e.complexity.Session.IPAddress(childComplexity), true
|
||||||
|
case "Session.identityId":
|
||||||
|
if e.complexity.Session.IdentityID == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.complexity.Session.IdentityID(childComplexity), true
|
||||||
case "Session.updatedAt":
|
case "Session.updatedAt":
|
||||||
if e.complexity.Session.UpdatedAt == nil {
|
if e.complexity.Session.UpdatedAt == nil {
|
||||||
break
|
break
|
||||||
@@ -2427,6 +2434,7 @@ type Invitation implements Node {
|
|||||||
|
|
||||||
type Session implements Node {
|
type Session implements Node {
|
||||||
id: ID!
|
id: ID!
|
||||||
|
identityId: ID!
|
||||||
ipAddress: String!
|
ipAddress: String!
|
||||||
userAgent: String!
|
userAgent: String!
|
||||||
updatedAt: Datetime!
|
updatedAt: Datetime!
|
||||||
@@ -6266,6 +6274,8 @@ func (ec *executionContext) fieldContext_Membership_lastSession(_ context.Contex
|
|||||||
switch field.Name {
|
switch field.Name {
|
||||||
case "id":
|
case "id":
|
||||||
return ec.fieldContext_Session_id(ctx, field)
|
return ec.fieldContext_Session_id(ctx, field)
|
||||||
|
case "identityId":
|
||||||
|
return ec.fieldContext_Session_identityId(ctx, field)
|
||||||
case "ipAddress":
|
case "ipAddress":
|
||||||
return ec.fieldContext_Session_ipAddress(ctx, field)
|
return ec.fieldContext_Session_ipAddress(ctx, field)
|
||||||
case "userAgent":
|
case "userAgent":
|
||||||
@@ -8476,6 +8486,8 @@ func (ec *executionContext) fieldContext_OrganizationSessionCreated_session(_ co
|
|||||||
switch field.Name {
|
switch field.Name {
|
||||||
case "id":
|
case "id":
|
||||||
return ec.fieldContext_Session_id(ctx, field)
|
return ec.fieldContext_Session_id(ctx, field)
|
||||||
|
case "identityId":
|
||||||
|
return ec.fieldContext_Session_identityId(ctx, field)
|
||||||
case "ipAddress":
|
case "ipAddress":
|
||||||
return ec.fieldContext_Session_ipAddress(ctx, field)
|
return ec.fieldContext_Session_ipAddress(ctx, field)
|
||||||
case "userAgent":
|
case "userAgent":
|
||||||
@@ -10842,6 +10854,35 @@ func (ec *executionContext) fieldContext_Session_id(_ context.Context, field gra
|
|||||||
return fc, nil
|
return fc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ec *executionContext) _Session_identityId(ctx context.Context, field graphql.CollectedField, obj *types.Session) (ret graphql.Marshaler) {
|
||||||
|
return graphql.ResolveField(
|
||||||
|
ctx,
|
||||||
|
ec.OperationContext,
|
||||||
|
field,
|
||||||
|
ec.fieldContext_Session_identityId,
|
||||||
|
func(ctx context.Context) (any, error) {
|
||||||
|
return obj.IdentityID, nil
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
ec.marshalNID2goᚗproboᚗincᚋproboᚋpkgᚋgidᚐGID,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ec *executionContext) fieldContext_Session_identityId(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
|
||||||
|
fc = &graphql.FieldContext{
|
||||||
|
Object: "Session",
|
||||||
|
Field: field,
|
||||||
|
IsMethod: false,
|
||||||
|
IsResolver: false,
|
||||||
|
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
|
||||||
|
return nil, errors.New("field of type ID does not have child fields")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return fc, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ec *executionContext) _Session_ipAddress(ctx context.Context, field graphql.CollectedField, obj *types.Session) (ret graphql.Marshaler) {
|
func (ec *executionContext) _Session_ipAddress(ctx context.Context, field graphql.CollectedField, obj *types.Session) (ret graphql.Marshaler) {
|
||||||
return graphql.ResolveField(
|
return graphql.ResolveField(
|
||||||
ctx,
|
ctx,
|
||||||
@@ -11116,6 +11157,8 @@ func (ec *executionContext) fieldContext_SessionEdge_node(_ context.Context, fie
|
|||||||
switch field.Name {
|
switch field.Name {
|
||||||
case "id":
|
case "id":
|
||||||
return ec.fieldContext_Session_id(ctx, field)
|
return ec.fieldContext_Session_id(ctx, field)
|
||||||
|
case "identityId":
|
||||||
|
return ec.fieldContext_Session_identityId(ctx, field)
|
||||||
case "ipAddress":
|
case "ipAddress":
|
||||||
return ec.fieldContext_Session_ipAddress(ctx, field)
|
return ec.fieldContext_Session_ipAddress(ctx, field)
|
||||||
case "userAgent":
|
case "userAgent":
|
||||||
@@ -11355,6 +11398,8 @@ func (ec *executionContext) fieldContext_SignInPayload_session(_ context.Context
|
|||||||
switch field.Name {
|
switch field.Name {
|
||||||
case "id":
|
case "id":
|
||||||
return ec.fieldContext_Session_id(ctx, field)
|
return ec.fieldContext_Session_id(ctx, field)
|
||||||
|
case "identityId":
|
||||||
|
return ec.fieldContext_Session_identityId(ctx, field)
|
||||||
case "ipAddress":
|
case "ipAddress":
|
||||||
return ec.fieldContext_Session_ipAddress(ctx, field)
|
return ec.fieldContext_Session_ipAddress(ctx, field)
|
||||||
case "userAgent":
|
case "userAgent":
|
||||||
@@ -17539,6 +17584,11 @@ func (ec *executionContext) _Session(ctx context.Context, sel ast.SelectionSet,
|
|||||||
if out.Values[i] == graphql.Null {
|
if out.Values[i] == graphql.Null {
|
||||||
out.Invalids++
|
out.Invalids++
|
||||||
}
|
}
|
||||||
|
case "identityId":
|
||||||
|
out.Values[i] = ec._Session_identityId(ctx, field, obj)
|
||||||
|
if out.Values[i] == graphql.Null {
|
||||||
|
out.Invalids++
|
||||||
|
}
|
||||||
case "ipAddress":
|
case "ipAddress":
|
||||||
out.Values[i] = ec._Session_ipAddress(ctx, field, obj)
|
out.Values[i] = ec._Session_ipAddress(ctx, field, obj)
|
||||||
if out.Values[i] == graphql.Null {
|
if out.Values[i] == graphql.Null {
|
||||||
|
|||||||
@@ -61,11 +61,12 @@ func NewSessionEdge(session *coredata.Session, orderField coredata.SessionOrderF
|
|||||||
|
|
||||||
func NewSession(session *coredata.Session) *Session {
|
func NewSession(session *coredata.Session) *Session {
|
||||||
return &Session{
|
return &Session{
|
||||||
ID: session.ID,
|
ID: session.ID,
|
||||||
IPAddress: session.IPAddress.String(),
|
IPAddress: session.IPAddress.String(),
|
||||||
UserAgent: session.UserAgent,
|
IdentityID: session.UserID,
|
||||||
UpdatedAt: session.UpdatedAt,
|
UserAgent: session.UserAgent,
|
||||||
CreatedAt: session.CreatedAt,
|
UpdatedAt: session.UpdatedAt,
|
||||||
ExpiresAt: session.ExpiredAt,
|
CreatedAt: session.CreatedAt,
|
||||||
|
ExpiresAt: session.ExpiredAt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -432,12 +432,13 @@ type SSOAvailability struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
ID gid.GID `json:"id"`
|
ID gid.GID `json:"id"`
|
||||||
IPAddress string `json:"ipAddress"`
|
IdentityID gid.GID `json:"identityId"`
|
||||||
UserAgent string `json:"userAgent"`
|
IPAddress string `json:"ipAddress"`
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
UserAgent string `json:"userAgent"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
ExpiresAt time.Time `json:"expiresAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
ExpiresAt time.Time `json:"expiresAt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Session) IsNode() {}
|
func (Session) IsNode() {}
|
||||||
|
|||||||
@@ -160,6 +160,11 @@ func (r *membershipResolver) LastSession(ctx context.Context, obj *types.Members
|
|||||||
|
|
||||||
childSession, err := r.iam.SessionService.GetActiveSessionForMembership(ctx, session.ID, obj.ID)
|
childSession, err := r.iam.SessionService.GetActiveSessionForMembership(ctx, session.ID, obj.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var errSessionNotFound *iam.ErrSessionNotFound
|
||||||
|
if errors.As(err, &errSessionNotFound) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
panic(fmt.Errorf("cannot get active session for membership: %w", err))
|
panic(fmt.Errorf("cannot get active session for membership: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user