feat: support concurrent Reddit accounts
This commit is contained in:
1 parent
d819a0f1e6
commit
16a31335d0
51 files changed
+3241
-250
No files matched your search
+55
-4
@@ -46,6 +46,18 @@ export function createLoginRouter(ctx: WebContext): Router {
|
||||
const router = Router();
|
||||
|
||||
router.get('/login/status', async (_req, res) => {
|
||||
const runningAccounts = ctx.services.redditAccounts?.list().filter((account) => account.isRunning) ?? [];
|
||||
if (runningAccounts.length > 0) {
|
||||
const only = runningAccounts.length === 1 ? runningAccounts[0] : null;
|
||||
res.json({
|
||||
ready: true,
|
||||
loggedIn: true,
|
||||
status: 'authenticated',
|
||||
accountId: only?.accountId ?? null,
|
||||
username: only?.redditUsername ?? null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const browser = ctx.services.browser;
|
||||
if (!browser) {
|
||||
res.status(503).json({ ready: false, error: 'browser not initialised' });
|
||||
@@ -57,9 +69,16 @@ export function createLoginRouter(ctx: WebContext): Router {
|
||||
? await accountAuth.ensureFullyAuthenticated()
|
||||
: await browser.isSessionValid();
|
||||
if (fullyAuthenticated) {
|
||||
const username = accountAuth?.getSnapshot().redditUsername
|
||||
const snapshot = accountAuth?.getSnapshot();
|
||||
const username = snapshot?.redditUsername
|
||||
?? await browser.getAuthenticatedUsername();
|
||||
res.json({ ready: true, loggedIn: true, status: 'authenticated', username });
|
||||
res.json({
|
||||
ready: true,
|
||||
loggedIn: true,
|
||||
status: 'authenticated',
|
||||
...(snapshot?.accountId ? { accountId: snapshot.accountId } : {}),
|
||||
username,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -97,9 +116,16 @@ export function createLoginRouter(ctx: WebContext): Router {
|
||||
}
|
||||
|
||||
try {
|
||||
await importer.import(cookies.map(toPlaywrightCookie), payload.data.matrix);
|
||||
const accounts = ctx.services.redditAccounts;
|
||||
if (!accounts) {
|
||||
await importer.import(cookies.map(toPlaywrightCookie), payload.data.matrix);
|
||||
ctx.logger.info(`Imported ${cookies.length} Reddit session cookies and verified the browser session`);
|
||||
res.json({ ok: true, status: 'authenticated' });
|
||||
return;
|
||||
}
|
||||
const account = await accounts.importSession(cookies.map(toPlaywrightCookie), payload.data.matrix);
|
||||
ctx.logger.info(`Imported ${cookies.length} Reddit session cookies and verified the browser session`);
|
||||
res.json({ ok: true, status: 'authenticated' });
|
||||
res.json({ ok: true, status: 'authenticated', account });
|
||||
} catch (error) {
|
||||
ctx.logger.error('Failed to import Reddit session cookies:', error);
|
||||
const message = error instanceof Error ? error.message : 'Could not import Reddit session';
|
||||
@@ -107,5 +133,30 @@ export function createLoginRouter(ctx: WebContext): Router {
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/accounts', (_req, res) => {
|
||||
const accounts = ctx.services.redditAccounts;
|
||||
if (!accounts) {
|
||||
res.status(503).json({ error: 'Reddit account service not initialised' });
|
||||
return;
|
||||
}
|
||||
res.json(accounts.list());
|
||||
});
|
||||
|
||||
router.post('/accounts/:accountId/activate', async (req, res) => {
|
||||
const accounts = ctx.services.redditAccounts;
|
||||
if (!accounts) {
|
||||
res.status(503).json({ ok: false, error: 'Reddit account service not initialised' });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const account = await accounts.activate(decodeURIComponent(req.params.accountId));
|
||||
ctx.logger.info(`Activated Reddit account u/${account.redditUsername}`);
|
||||
res.json({ ok: true, account });
|
||||
} catch (error) {
|
||||
ctx.logger.error('Failed to activate Reddit account:', error);
|
||||
res.status(401).json({ ok: false, error: error instanceof Error ? error.message : 'Could not activate Reddit account' });
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in new issue
Block a user