Skip to content

Commit abb191f

Browse files
Hashed user ID cookie improvements (#20232)
* refetch JWT cookie 20s after load * set cookie on login * make the cookie trigger at the right place
1 parent 92f3ec0 commit abb191f

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

components/dashboard/src/user-context.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ const UserContext = createContext<{
1818
setUser: () => null,
1919
});
2020

21+
const refetchCookie = async () => {
22+
await fetch("/api/auth/jwt-cookie", {
23+
credentials: "include",
24+
})
25+
.then((resp) => resp.text())
26+
.then((text) => console.log(`Completed JWT Cookie refresh: ${text}`))
27+
.catch((err) => {
28+
console.log("Failed to update jwt-cookie", err);
29+
});
30+
};
31+
2132
const UserContextProvider: React.FC = ({ children }) => {
2233
const [user, setUser] = useState<User>();
2334

@@ -46,16 +57,9 @@ const UserContextProvider: React.FC = ({ children }) => {
4657
const frequencyMs = 1000 * 60 * 5; // 5 mins
4758
if (!_gp.jwttimer) {
4859
// Store the timer on the window, to avoid queuing up multiple
49-
_gp.jwtTimer = setInterval(() => {
50-
fetch("/api/auth/jwt-cookie", {
51-
credentials: "include",
52-
})
53-
.then((resp) => resp.text())
54-
.then((text) => console.log(`Completed JWT Cookie refresh: ${text}`))
55-
.catch((err) => {
56-
console.log("Failed to update jwt-cookie", err);
57-
});
58-
}, frequencyMs);
60+
_gp.jwtTimer = setInterval(refetchCookie, frequencyMs);
61+
62+
setTimeout(refetchCookie, 20_000);
5963
}
6064
},
6165
[user, client],

components/server/src/auth/login-completion-handler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export class LoginCompletionHandler {
103103
// (default case) If we got redirected here onto the base domain of the Gitpod installation, we can just issue the cookie right away.
104104
const cookie = await this.session.createJWTSessionCookie(user.id);
105105
response.cookie(cookie.name, cookie.value, cookie.opts);
106+
this.session.setHashedUserIdCookie(request, response);
106107
reportJWTCookieIssued();
107108

108109
log.info(logContext, `User is logged in successfully. Redirect to: ${returnTo}`);

components/server/src/session-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class SessionHandler {
241241
});
242242
}
243243

244-
private setHashedUserIdCookie(req: express.Request, res: express.Response): void {
244+
public setHashedUserIdCookie(req: express.Request, res: express.Response): void {
245245
const user = req.user as User;
246246
if (!user) return;
247247

0 commit comments

Comments
 (0)