Skip to content

Commit d5208cb

Browse files
refactor: Update setHashedUserIdCookie method in analytics controller (#20229)
* 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 * nit fix :) Co-authored-by: Filip Troníček <[email protected]> --------- Co-authored-by: Filip Troníček <[email protected]>
1 parent 192727f commit d5208cb

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

components/server/src/analytics-controller.ts

Lines changed: 27 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,33 @@ 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 = `.${hostname}`;
202+
203+
res.cookie("gitpod_hashed_user_id", hashedUserId, {
204+
domain: domain,
205+
maxAge: oneYearInSeconds * 1000, // Convert to milliseconds
206+
httpOnly: true,
207+
secure: true,
208+
sameSite: "lax",
209+
});
210+
}
193211
}
194212
}

0 commit comments

Comments
 (0)