diff --git a/new-login-and-save/login2.html b/new-login-and-save/login2.html index 17b8f3fc..50807655 100644 --- a/new-login-and-save/login2.html +++ b/new-login-and-save/login2.html @@ -23,7 +23,7 @@

Sign Up / Login (Filesystem)

- +
@@ -65,46 +65,24 @@ const filesListEl = document.getElementById('filesList'); const fileActionsEl = document.getElementById('fileActions'); - // Helper: sign in and return the user's Public cookies directory path - async function getUserPublicDir() { - // This will prompt sign-in if the user isn't authenticated yet. - const user = await puter.auth.signIn(); - if (!user) throw new Error('Sign in required.'); - - // Derive a username from common fields returned by auth - const username = user.username || user.handle || user.name || user.id; - if (!username) throw new Error('Could not determine username from signed-in user.'); - - // Build the user's Public/cookies directory - return `${username}/Public/cookies`; - } + const DIR = 'cookies'; async function ensureDir() { - msg.textContent = 'Ensuring your Public/cookies directory...'; try { - const dir = await getUserPublicDir(); - // Ensure parent Public exists and cookies subdir exists. - // Some implementations will create nested dirs, but create both to be safe. - const parts = dir.split('/'); - if (parts.length >= 2) { - const parent = parts.slice(0, parts.length - 1).join('/'); - try { await puter.fs.mkdir(parent); } catch (e) { /* ignore if exists */ } - } - try { - await puter.fs.mkdir(dir); - msg.textContent = `Ensured directory: /${dir}`; - } catch (e) { - // If mkdir fails because it exists, readdir to confirm - try { - await puter.fs.readdir(dir); - msg.textContent = `Directory already exists: /${dir}`; - } catch (err) { - msg.textContent = 'Error ensuring directory: ' + (err.message || err); - throw err; - } - } + // Try creating the directory; if it exists, some implementations may throw - handle gracefully + await puter.fs.mkdir(DIR); + msg.textContent = `Ensured directory: ${DIR}`; } catch (e) { - msg.textContent = 'Error ensuring directory: ' + (e.message || e); + // If the error indicates directory exists, ignore + // Otherwise, show message + // Many runtime implementations return an error - just attempt readdir to confirm existence + try { + await puter.fs.readdir(DIR); + msg.textContent = `Directory already exists: ${DIR}`; + } catch (err) { + msg.textContent = 'Error ensuring directory: ' + (err.message || err); + throw err; + } } } @@ -112,18 +90,7 @@ filesListEl.textContent = 'Loading...'; fileActionsEl.innerHTML = ''; try { - const dir = await (async () => { - try { return await getUserPublicDir(); } - catch (e) { - // If user cancels sign-in or we can't sign in, show helpful message - msg.textContent = 'Sign in to view your saved cookie files.'; - filesListEl.textContent = 'Sign in to view files.'; - return null; - } - })(); - if (!dir) return []; - - const entries = await puter.fs.readdir(dir).catch(err => { + const entries = await puter.fs.readdir(DIR).catch(err => { // If directory doesn't exist, show empty list return null; }); @@ -168,18 +135,12 @@ try { const user = await puter.auth.signIn(); if (!user) throw new Error('Sign in failed.'); - const username = user.username || user.handle || user.name || user.id; - if (!username) throw new Error('Could not determine username.'); - - const dir = `${username}/Public/cookies`; // ensure dir exists - try { await puter.fs.mkdir(`${username}/Public`); } catch (e) {} - try { await puter.fs.mkdir(dir); } catch (e) {} - + await ensureDir(); const cookies = getCookies(); - const filename = `${dir}/cookies-${Date.now()}.txt`; + const filename = `${DIR}/cookies-${Date.now()}.txt`; await puter.fs.write(filename, JSON.stringify(cookies)); - msg.textContent = `Saved cookies to /${filename}`; + msg.textContent = `Saved cookies to ${filename}`; await listFiles(); } catch (e) { msg.textContent = 'Error saving file: ' + (e.message || e); @@ -191,9 +152,7 @@ try { const user = await puter.auth.signIn(); if (!user) throw new Error('Sign in failed.'); - const username = user.username || user.handle || user.name || user.id; - if (!username) throw new Error('Could not determine username.'); - const path = `${username}/Public/cookies/${filename}`; + const path = `${DIR}/${filename}`; const data = await puter.fs.read(path); if (!data) throw new Error('File empty or unreadable.'); let cookies; @@ -216,9 +175,7 @@ try { const user = await puter.auth.signIn(); if (!user) throw new Error('Sign in failed.'); - const username = user.username || user.handle || user.name || user.id; - if (!username) throw new Error('Could not determine username.'); - const path = `${username}/Public/cookies/${filename}`; + const path = `${DIR}/${filename}`; await puter.fs.delete(path); msg.textContent = `Deleted ${filename}.`; await listFiles(); @@ -228,21 +185,18 @@ } async function clearAllFiles() { - if (!confirm('Delete ALL cookie files in your Public/cookies?')) return; + if (!confirm('Delete ALL cookie files in cloud?')) return; msg.textContent = 'Signing in and deleting all cookie files...'; try { const user = await puter.auth.signIn(); if (!user) throw new Error('Sign in failed.'); - const username = user.username || user.handle || user.name || user.id; - if (!username) throw new Error('Could not determine username.'); - const dir = `${username}/Public/cookies`; - const entries = await puter.fs.readdir(dir).catch(() => null); + const entries = await puter.fs.readdir(DIR).catch(() => null); if (!entries || entries.length === 0) { msg.textContent = 'No files to delete.'; return; } for (const name of entries) { - await puter.fs.delete(`${dir}/${name}`).catch(err => { + await puter.fs.delete(`${DIR}/${name}`).catch(err => { // continue deleting others even if one fails console.warn('Failed deleting', name, err); }); @@ -260,9 +214,10 @@ document.getElementById('refresh').onclick = listFiles; document.getElementById('clearAll').onclick = clearAllFiles; - // Initial load: try to list files (will prompt sign-in if necessary) + // Initial load: ensure directory exists and list files (but don't require sign-in for listing) (async () => { try { + // Try to list files; if dir missing, show no files await listFiles(); } catch (e) { console.warn('Initial listing failed', e);