Skip to content

Commit c2d3f4f

Browse files
fix: resolve correct creator ip on oauth sign up (#2650)
## Summary - Add OAuth creator-context fallback to latest `auth.sessions` IP/user agent when signup metadata is missing. --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 477f546 commit c2d3f4f

5 files changed

Lines changed: 71 additions & 3 deletions

File tree

packages/dashboard-api/internal/teamprovision/creator_context.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
"fmt"
77

88
"github.com/google/uuid"
9+
"go.uber.org/zap"
910

1011
"github.com/e2b-dev/infra/packages/db/pkg/dberrors"
1112
supabasedb "github.com/e2b-dev/infra/packages/db/pkg/supabase"
13+
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
1214
sharedteamprovision "github.com/e2b-dev/infra/packages/shared/pkg/teamprovision"
15+
"github.com/e2b-dev/infra/packages/shared/pkg/utils"
1316
)
1417

1518
const (
@@ -23,7 +26,8 @@ const (
2326
)
2427

2528
// resolveCreatorContext reads signup IP/UA and auth provider from
26-
// auth.users.raw_app_meta_data, which Supabase populates for every signup flow.
29+
// auth.users.raw_app_meta_data and falls back to auth.sessions for social
30+
// providers when metadata is missing.
2731
// Returns nil when the user cannot be found so callers can keep going without
2832
// the optional context.
2933
func resolveCreatorContext(ctx context.Context, supabaseDB *supabasedb.Client, userID uuid.UUID) (*sharedteamprovision.CreatorContextV1, error) {
@@ -44,13 +48,36 @@ func resolveCreatorContext(ctx context.Context, supabaseDB *supabasedb.Client, u
4448
}
4549

4650
authMethod := sharedteamprovision.AuthMethodPassword
51+
ipAddress := stringFromMetadata(metadata, signupIPMetadataKey)
52+
userAgent := stringFromMetadata(metadata, signupUserAgentMetadataKey)
53+
4754
if hasOAuthProvider(metadata) {
4855
authMethod = sharedteamprovision.AuthMethodSocial
56+
57+
if ipAddress == "" || userAgent == "" {
58+
session, sessionErr := supabaseDB.Write.GetLatestAuthSessionByUserID(ctx, userID)
59+
60+
if sessionErr != nil {
61+
if !dberrors.IsNotFoundError(sessionErr) {
62+
logger.L().Warn(ctx, "failed to resolve latest auth session for creator context, falling back to metadata",
63+
zap.String("user_id", userID.String()),
64+
zap.Error(sessionErr),
65+
)
66+
}
67+
} else {
68+
if ipAddress == "" {
69+
ipAddress = utils.DerefOrDefault(session.Ip, "")
70+
}
71+
if userAgent == "" {
72+
userAgent = utils.DerefOrDefault(session.UserAgent, "")
73+
}
74+
}
75+
}
4976
}
5077

5178
return &sharedteamprovision.CreatorContextV1{
52-
IPAddress: stringFromMetadata(metadata, signupIPMetadataKey),
53-
UserAgent: stringFromMetadata(metadata, signupUserAgentMetadataKey),
79+
IPAddress: ipAddress,
80+
UserAgent: userAgent,
5481
AuthMethod: authMethod,
5582
}, nil
5683
}

packages/db/pkg/supabase/queries/get_auth_user.sql.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/db/pkg/supabase/queries/models.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/db/pkg/supabase/schema/auth_users_override.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ CREATE TABLE auth.users (
1818
raw_app_meta_data jsonb,
1919
PRIMARY KEY (id)
2020
);
21+
22+
CREATE TABLE auth.sessions (
23+
user_id uuid NOT NULL,
24+
created_at timestamptz NOT NULL DEFAULT now(),
25+
user_agent text,
26+
ip text
27+
);

packages/db/pkg/supabase/sql_queries/users/get_auth_user.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22
SELECT id, COALESCE(email, '') AS email, created_at, COALESCE(raw_app_meta_data, '{}'::jsonb) AS raw_app_meta_data
33
FROM auth.users
44
WHERE id = $1::uuid;
5+
6+
-- name: GetLatestAuthSessionByUserID :one
7+
SELECT user_agent, ip
8+
FROM auth.sessions
9+
WHERE user_id = $1::uuid
10+
ORDER BY created_at DESC
11+
LIMIT 1;

0 commit comments

Comments
 (0)