Skip to content

Commit 7c12471

Browse files
committed
cache leetcode cookie jar into tornado application setting
1 parent 7ad0a61 commit 7c12471

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

jupyterlab_leetcode/handlers/cookie_handler.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,18 @@ def get(self):
4848
cast(Cookie, cookie_session).is_expired()
4949
or cast(Cookie, cookie_csrf).is_expired()
5050
)
51+
checked = exist and not expired
5152

52-
resp = {"exist": exist, "expired": expired}
53+
resp = {"exist": exist, "expired": expired, "checked": checked}
5354

54-
if exist and not expired:
55+
if checked:
5556
cookie_session_expires = cast(Cookie, cookie_session).expires
5657
max_age = (
5758
cookie_session_expires - int(time.time())
5859
if cookie_session_expires is not None
5960
else 3600 * 24 * 14
6061
)
61-
self.add_header(
62-
"Set-Cookie",
63-
f"leetcode_browser={browser}; Path=/; Max-Age={max_age}",
64-
)
62+
self.set_cookie("leetcode_browser", browser, max_age=max_age)
63+
self.settings.update(leetcode_browser=browser, leetcode_cookiejar=cj)
6564

6665
self.finish(json.dumps(resp))

src/components/BrowserCookie.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ const BrowserCookie = ({
6161
return;
6262
}
6363

64-
getCookie(browser).then(cookies => {
65-
setChecked(!!cookies['exist']);
64+
getCookie(browser).then(resp => {
65+
setChecked(resp['checked']);
6666
});
6767
};
6868

src/widget.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ReactWidget } from '@jupyterlab/ui-components';
22
import React, { StrictMode, useEffect, useState } from 'react';
33
import LandingPage from './components/LandingPage';
44
import LeetCode from './components/LeetCode';
5+
import { getCookie } from './services/cookie';
56

67
const LeetCodeComponent = (): JSX.Element => {
78
const [cookieLoggedIn, setCookieLoggedIn] = useState('');
@@ -12,9 +13,13 @@ const LeetCodeComponent = (): JSX.Element => {
1213
.find(cookie => cookie.startsWith('leetcode_browser='))
1314
?.split('=')[1];
1415
if (leetcode_browser) {
15-
setCookieLoggedIn(leetcode_browser);
16+
getCookie(leetcode_browser).then(resp => {
17+
if (resp['checked']) {
18+
setCookieLoggedIn(leetcode_browser);
19+
}
20+
});
1621
}
17-
}, []);
22+
});
1823

1924
return cookieLoggedIn ? (
2025
<LeetCode />

0 commit comments

Comments
 (0)