Spaces:
Sleeping
Sleeping
Update login.js
Browse files
login.js
CHANGED
|
@@ -6,12 +6,6 @@ const DIR = process.env.STATE_DIR || '/data';
|
|
| 6 |
const COOKIE_FILE = path.join(DIR, 'state.json');
|
| 7 |
const LOG_DIR = path.join(DIR, 'log');
|
| 8 |
|
| 9 |
-
/* make sure the directory tree exists */
|
| 10 |
-
fs.mkdirSync(DIR, { recursive: true });
|
| 11 |
-
|
| 12 |
-
/* ---------- save cookie ---------- */
|
| 13 |
-
await context.storageState({ path: COOKIE_FILE });
|
| 14 |
-
|
| 15 |
/**
|
| 16 |
* Launch Playwright, sign in to n8n if needed, refresh cookie,
|
| 17 |
* save a screenshot, return status object.
|
|
@@ -19,16 +13,18 @@ await context.storageState({ path: COOKIE_FILE });
|
|
| 19 |
export async function runLogin() {
|
| 20 |
let browser;
|
| 21 |
try {
|
|
|
|
|
|
|
|
|
|
| 22 |
browser = await chromium.launch({ args: ['--no-sandbox'] });
|
| 23 |
|
| 24 |
/* ---------- reuse cookie ---------- */
|
| 25 |
const hasCookie = fs.existsSync(COOKIE_FILE);
|
| 26 |
-
const context =
|
| 27 |
-
?
|
| 28 |
-
:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
const page = await context.newPage();
|
| 33 |
const baseUrl = process.env.N8N_URL || 'http://localhost:5678/';
|
| 34 |
await page.goto(baseUrl, { waitUntil: 'networkidle', timeout: 60000 });
|
|
@@ -71,13 +67,14 @@ export async function runLogin() {
|
|
| 71 |
|
| 72 |
/* save cookie */
|
| 73 |
await context.storageState({ path: COOKIE_FILE });
|
| 74 |
-
|
| 75 |
-
fs.mkdirSync(LOG_DIR, { recursive: true });
|
| 76 |
const ts = new Date().toISOString().replace(/[:T]/g, '-').split('.')[0];
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
return { ok: true };
|
| 81 |
} catch (err) {
|
| 82 |
console.error('❌ runLogin error:', err);
|
| 83 |
return { ok: false, error: err.message };
|
|
|
|
| 6 |
const COOKIE_FILE = path.join(DIR, 'state.json');
|
| 7 |
const LOG_DIR = path.join(DIR, 'log');
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
/**
|
| 10 |
* Launch Playwright, sign in to n8n if needed, refresh cookie,
|
| 11 |
* save a screenshot, return status object.
|
|
|
|
| 13 |
export async function runLogin() {
|
| 14 |
let browser;
|
| 15 |
try {
|
| 16 |
+
/* ---------- ensure writable paths exist ---------- */
|
| 17 |
+
fs.mkdirSync(LOG_DIR, { recursive: true }); // also creates DIR
|
| 18 |
+
|
| 19 |
browser = await chromium.launch({ args: ['--no-sandbox'] });
|
| 20 |
|
| 21 |
/* ---------- reuse cookie ---------- */
|
| 22 |
const hasCookie = fs.existsSync(COOKIE_FILE);
|
| 23 |
+
const context = await browser.newContext({
|
| 24 |
+
storageState: hasCookie ? COOKIE_FILE : undefined,
|
| 25 |
+
ignoreHTTPSErrors: true, // in case your n8n URL uses self-signed cert
|
| 26 |
+
});
|
| 27 |
+
|
|
|
|
| 28 |
const page = await context.newPage();
|
| 29 |
const baseUrl = process.env.N8N_URL || 'http://localhost:5678/';
|
| 30 |
await page.goto(baseUrl, { waitUntil: 'networkidle', timeout: 60000 });
|
|
|
|
| 67 |
|
| 68 |
/* save cookie */
|
| 69 |
await context.storageState({ path: COOKIE_FILE });
|
| 70 |
+
|
|
|
|
| 71 |
const ts = new Date().toISOString().replace(/[:T]/g, '-').split('.')[0];
|
| 72 |
+
const shot = path.join(LOG_DIR, `overview-${ts}.png`);
|
| 73 |
+
await page.screenshot({ path: shot, fullPage: true });
|
| 74 |
+
|
| 75 |
+
await context.tracing.stop({ path: path.join(DIR, 'trace.zip') });
|
| 76 |
|
| 77 |
+
return { ok: true, screenshot: shot, loggedIn: needSignIn ? 'fresh' : 'cookie' };
|
|
|
|
| 78 |
} catch (err) {
|
| 79 |
console.error('❌ runLogin error:', err);
|
| 80 |
return { ok: false, error: err.message };
|