Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
add xspace-agent/x-spaces/auth.js
oversteppingc1da754a386e
diff --git a/xspace-agent/x-spaces/auth.js b/xspace-agent/x-spaces/auth.jsnew file mode 100644index 000000000..d2d4f4bbd--- /dev/null+++ b/xspace-agent/x-spaces/auth.js@@ -0,0 +1,150 @@+// SPDX-License-Identifier: Apache-2.0+// Copyright 2026 nirholas (https://github.com/nirholas/xspace-agent) [§68]++// DEPRECATED: Use packages/core/src/browser/ instead.+// Will be removed in v1.0.++const { saveCookies } = require("./browser")++const X_USERNAME = process.env.X_USERNAME+const X_PASSWORD = process.env.X_PASSWORD+const X_EMAIL = process.env.X_EMAIL || ""+const X_AUTH_TOKEN = process.env.X_AUTH_TOKEN || ""+const X_CT0 = process.env.X_CT0 || ""++async function isLoggedIn(page) {+ try {+ await page.goto("https://x.com/home", { waitUntil: "networkidle2", timeout: 15000 })+ const url = page.url()+ return url.includes("/home") && !url.includes("/login")+ } catch {+ return false+ }+}++async function login(page, emitter) {+ // Check if already logged in via saved cookies+ if (await isLoggedIn(page)) {+ console.log("[X-Spaces] Already logged in via cookies")+ emitter.emit("status", "logged-in")+ return true+ }++ // Fast path: inject auth_token directly if provided in .env+ if (X_AUTH_TOKEN) {+ console.log("[X-Spaces] Logging in via X_AUTH_TOKEN cookie")+ emitter.emit("status", "logging-in")+ const cookiesToSet = [+ { name: "auth_token", value: X_AUTH_TOKEN, domain: ".x.com", path: "/", secure: true, httpOnly: true, sameSite: "None" }+ ]+ if (X_CT0) cookiesToSet.push({ name: "ct0", value: X_CT0, domain: ".x.com", path: "/", secure: false, httpOnly: false, sameSite: "Lax" })+ await page.setCookie(...cookiesToSet)+ const ok = await isLoggedIn(page)+ if (!ok) throw new Error("X_AUTH_TOKEN is invalid or expired — get a fresh one from your browser")+ console.log("[X-Spaces] Login successful via auth_token!")+ await saveCookies(page)+ emitter.emit("status", "logged-in")+ return true+ }++ if (!X_USERNAME || !X_PASSWORD) {+ throw new Error("Set X_AUTH_TOKEN (recommended) or X_USERNAME + X_PASSWORD in .env")+ }++ console.log("[X-Spaces] Logging in via browser form as:", X_USERNAME)+ emitter.emit("status", "logging-in")++ await page.goto("https://x.com/i/flow/login", { waitUntil: "networkidle2", timeout: 30000 })+ await delay(2000)++ // Step 1: Username+ await page.waitForSelector('input[autocomplete="username"]', { timeout: 15000 })+ await delay(500)+ await page.type('input[autocomplete="username"]', X_USERNAME, { delay: randomDelay() })+ await delay(500)+ await clickButtonByText(page, "Next")+ await delay(2000)++ // Step 2: Email/phone verification (if X asks for it)+ const verifyInput = await page.$('input[data-testid="ocfEnterTextTextInput"]')+ if (verifyInput) {+ console.log("[X-Spaces] Verification step — entering email/phone")+ const val = X_EMAIL || X_USERNAME+ await verifyInput.type(val, { delay: randomDelay() })+ await delay(500)+ const verifyNext = await page.$('[data-testid="ocfEnterTextNextButton"]')+ if (verifyNext) await verifyNext.click()+ await delay(2000)+ }++ // Step 3: Password+ let passwordInput+ try {+ passwordInput = await page.waitForSelector('input[name="password"], input[type="password"]', { timeout: 15000 })+ } catch {+ await page.screenshot({ path: "/tmp/x-login-step3.png" })+ throw new Error("Password field not found — X may be blocking the login. Try setting X_AUTH_TOKEN instead. Screenshot: /tmp/x-login-step3.png")+ }+ await delay(500)+ await passwordInput.type(X_PASSWORD, { delay: randomDelay() })+ await delay(500)++ // Click Log in+ const loginBtn = await page.$('[data-testid="LoginForm_Login_Button"]')+ if (loginBtn) {+ await loginBtn.click()+ } else {+ await clickButtonByText(page, "Log in")+ }+ await delay(3000)++ // 2FA+ const twoFaInput = await page.$('input[data-testid="ocfEnterTextTextInput"]')+ if (twoFaInput && page.url().includes("two_factor")) {+ console.log("[X-Spaces] 2FA required")+ emitter.emit("2fa-required", {})+ const code = await new Promise((resolve, reject) => {+ const timeout = setTimeout(() => reject(new Error("2FA timeout (120s)")), 120000)+ emitter.once("2fa-code", c => { clearTimeout(timeout); resolve(c) })+ })+ await twoFaInput.type(code, { delay: 50 })+ const verifyBtn = await page.$('[data-testid="ocfEnterTextNextButton"]')+ if (verifyBtn) await verifyBtn.click()+ await delay(3000)+ }++ const loggedIn = await isLoggedIn(page)+ if (!loggedIn) {+ await page.screenshot({ path: "/tmp/x-login-failed.png" })+ throw new Error("Login failed — screenshot at /tmp/x-login-failed.png. Consider using X_AUTH_TOKEN instead.")+ }++ console.log("[X-Spaces] Login successful!")+ await saveCookies(page)+ emitter.emit("status", "logged-in")+ return true+}++async function clickButtonByText(page, text) {+ const buttons = await page.$$('button[role="button"], [role="button"]')+ for (const btn of buttons) {+ const t = await page.evaluate(el => el.textContent?.trim() || "", btn)+ if (t.includes(text)) {+ await btn.click()+ return true+ }+ }+ return false+}++function delay(ms) {+ return new Promise(r => setTimeout(r, ms))+}++function randomDelay() {+ return 30 + Math.random() * 70+}++module.exports = { login, isLoggedIn }++