fix: verify stored sessions from a Reddit page
This commit is contained in:
1 parent
8df2d648dd
commit
5623100e93
2 files changed
+26
-1
No files matched your search
+17
-1
@@ -133,7 +133,14 @@ export class BrowserClient {
|
||||
/** Returns the username and stable t2 account ID for identity binding. */
|
||||
async getAuthenticatedIdentity(): Promise<RedditAccountIdentity | null> {
|
||||
try {
|
||||
return await this.getRedditIdentity(await this.getPage());
|
||||
const page = await this.getPage();
|
||||
if (!isRedditPage(page.url())) {
|
||||
await page.goto('https://www.reddit.com', {
|
||||
waitUntil: 'domcontentloaded',
|
||||
timeout: 30000,
|
||||
});
|
||||
}
|
||||
return await this.getRedditIdentity(page);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
@@ -212,3 +219,12 @@ export class BrowserClient {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function isRedditPage(value: string): boolean {
|
||||
try {
|
||||
const hostname = new URL(value).hostname.toLowerCase();
|
||||
return hostname === 'reddit.com' || hostname.endsWith('.reddit.com');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,11 @@ describe('BrowserClient', () => {
|
||||
json: async () => ({ kind: 't2', data: { name: 'bot-account', id: 'abc123' } }),
|
||||
});
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
let pageUrl = 'about:blank';
|
||||
const goto = vi.fn(async (url: string) => { pageUrl = url; });
|
||||
const page = {
|
||||
url: () => pageUrl,
|
||||
goto,
|
||||
evaluate: async (pageFunction: () => Promise<string | null>) => pageFunction(),
|
||||
} as unknown as Page;
|
||||
const context = { pages: () => [page] } as unknown as BrowserContext;
|
||||
@@ -54,6 +58,11 @@ describe('BrowserClient', () => {
|
||||
username: 'bot-account',
|
||||
accountId: 't2_abc123',
|
||||
});
|
||||
expect(goto).toHaveBeenCalledTimes(1);
|
||||
expect(goto).toHaveBeenCalledWith('https://www.reddit.com', {
|
||||
waitUntil: 'domcontentloaded',
|
||||
timeout: 30000,
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
'https://www.reddit.com/api/me.json',
|
||||
{ credentials: 'include' },
|
||||
|
||||
Reference in new issue
Block a user