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. */
|
/** Returns the username and stable t2 account ID for identity binding. */
|
||||||
async getAuthenticatedIdentity(): Promise<RedditAccountIdentity | null> {
|
async getAuthenticatedIdentity(): Promise<RedditAccountIdentity | null> {
|
||||||
try {
|
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 {
|
} catch {
|
||||||
return null;
|
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' } }),
|
json: async () => ({ kind: 't2', data: { name: 'bot-account', id: 'abc123' } }),
|
||||||
});
|
});
|
||||||
vi.stubGlobal('fetch', fetchMock);
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
|
let pageUrl = 'about:blank';
|
||||||
|
const goto = vi.fn(async (url: string) => { pageUrl = url; });
|
||||||
const page = {
|
const page = {
|
||||||
|
url: () => pageUrl,
|
||||||
|
goto,
|
||||||
evaluate: async (pageFunction: () => Promise<string | null>) => pageFunction(),
|
evaluate: async (pageFunction: () => Promise<string | null>) => pageFunction(),
|
||||||
} as unknown as Page;
|
} as unknown as Page;
|
||||||
const context = { pages: () => [page] } as unknown as BrowserContext;
|
const context = { pages: () => [page] } as unknown as BrowserContext;
|
||||||
@@ -54,6 +58,11 @@ describe('BrowserClient', () => {
|
|||||||
username: 'bot-account',
|
username: 'bot-account',
|
||||||
accountId: 't2_abc123',
|
accountId: 't2_abc123',
|
||||||
});
|
});
|
||||||
|
expect(goto).toHaveBeenCalledTimes(1);
|
||||||
|
expect(goto).toHaveBeenCalledWith('https://www.reddit.com', {
|
||||||
|
waitUntil: 'domcontentloaded',
|
||||||
|
timeout: 30000,
|
||||||
|
});
|
||||||
expect(fetchMock).toHaveBeenCalledWith(
|
expect(fetchMock).toHaveBeenCalledWith(
|
||||||
'https://www.reddit.com/api/me.json',
|
'https://www.reddit.com/api/me.json',
|
||||||
{ credentials: 'include' },
|
{ credentials: 'include' },
|
||||||
|
|||||||
Reference in new issue
Block a user