Skip to content

Commit 0434266

Browse files
refactor: Update setHashedUserIdCookie method in analytics controller
This commit updates the setHashedUserIdCookie method in the analytics controller. The changes include: - Adding a server-side implementation to determine the appropriate domain for setting the cookie based on the request hostname
1 parent 192727f commit 0434266

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

components/server/src/analytics-controller.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class AnalyticsController {
7070
const clientHeaderFields = toClientHeaderFields(req);
7171
const event = req.body as RemoteIdentifyMessage;
7272
this.identifyUser(req.user.id, event, clientHeaderFields);
73-
this.setHashedUserIdCookie(req.user.id, res);
73+
this.setHashedUserIdCookie(req.user.id, req, res);
7474
res.sendStatus(200);
7575
} catch (e) {
7676
console.error("failed to identify user", e);
@@ -180,15 +180,40 @@ export class AnalyticsController {
180180
}
181181
}
182182

183-
private setHashedUserIdCookie(userId: string, res: express.Response): void {
183+
private setHashedUserIdCookie(userId: string, req: express.Request, res: express.Response): void {
184184
const hashedUserId = crypto.createHash("md5").update(userId).digest("hex");
185185
const oneYearInSeconds = 365 * 24 * 60 * 60;
186-
res.cookie("gitpod_hashed_user_id", hashedUserId, {
187-
domain: ".gitpod.io",
188-
maxAge: oneYearInSeconds * 1000, // Convert to milliseconds
189-
httpOnly: true,
190-
secure: true,
191-
sameSite: "lax",
192-
});
186+
187+
/**
188+
* This implementation is inspired by isGitpodIo() from /workspace/gitpod/components/dashboard/src/utils.ts
189+
* We're using a server-side equivalent here because:
190+
* 1. The original function is client-side code using window.location
191+
* 2. This is server-side code that needs to use the request object
192+
* 3. We need to determine the appropriate domain for setting the cookie
193+
*/
194+
const hostname = req.hostname;
195+
if (
196+
hostname === "gitpod.io" ||
197+
hostname === "gitpod-staging.com" ||
198+
hostname.endsWith("gitpod-dev.com") ||
199+
hostname.endsWith("gitpod-io-dev.com")
200+
) {
201+
const domain =
202+
hostname === "gitpod.io"
203+
? ".gitpod.io"
204+
: hostname === "gitpod-staging.com"
205+
? ".gitpod-staging.com"
206+
: hostname.endsWith("gitpod-dev.com")
207+
? ".gitpod-dev.com"
208+
: ".gitpod-io-dev.com";
209+
210+
res.cookie("gitpod_hashed_user_id", hashedUserId, {
211+
domain: domain,
212+
maxAge: oneYearInSeconds * 1000, // Convert to milliseconds
213+
httpOnly: true,
214+
secure: true,
215+
sameSite: "lax",
216+
});
217+
}
193218
}
194219
}

0 commit comments

Comments
 (0)