@dongdev/fca-unofficial 3.0.9 → 3.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -155,3 +155,6 @@ Too lazy to write changelog, sorry! (will write changelog in the next release, t
155
155
 
156
156
  ## v3.0.8 - 2025-11-27
157
157
  - Hotfix / auto bump
158
+
159
+ ## v3.0.9 - 2025-12-05
160
+ - Hotfix / auto bump
@@ -471,13 +471,30 @@ async function tryAutoLoginIfNeeded(currentHtml, currentCookies, globalOptions,
471
471
  cs.find(c => c.key === "c_user")?.value ||
472
472
  cs.find(c => c.name === "i_user")?.value ||
473
473
  cs.find(c => c.name === "c_user")?.value;
474
+ const htmlUID = body => {
475
+ const s = typeof body === "string" ? body : String(body ?? "");
476
+ return s.match(/"USER_ID"\s*:\s*"(\d+)"/)?.[1] || s.match(/\["CurrentUserInitialData",\[\],\{.*?"USER_ID":"(\d+)".*?\},\d+\]/)?.[1];
477
+ };
474
478
  let userID = getUID(currentCookies);
479
+ // Also try to extract userID from HTML if not found in cookies
480
+ if (!userID) {
481
+ userID = htmlUID(currentHtml);
482
+ }
475
483
  if (userID) return { html: currentHtml, cookies: currentCookies, userID };
476
484
  // If appState/Cookie was provided and is not dead (not checkpointed), skip backup
477
485
  if (hadAppStateInput) {
478
486
  const isCheckpoint = currentHtml.includes("/checkpoint/block/?next");
479
487
  if (!isCheckpoint) {
480
- // AppState provided and not checkpointed, skip backup - just throw error
488
+ // AppState provided and not checkpointed, but userID not found
489
+ // This might be a temporary issue - try to refresh cookies from jar
490
+ try {
491
+ const refreshedCookies = await Promise.resolve(jar.getCookies("https://www.facebook.com"));
492
+ userID = getUID(refreshedCookies);
493
+ if (userID) {
494
+ return { html: currentHtml, cookies: refreshedCookies, userID };
495
+ }
496
+ } catch { }
497
+ // If still no userID, skip backup and throw error
481
498
  throw new Error("Missing user cookie from provided appState");
482
499
  }
483
500
  // AppState is dead (checkpointed), proceed to backup/email login
@@ -533,6 +550,26 @@ function makeLogin(j, email, password, globalOptions) {
533
550
  function loginHelper(appState, Cookie, email, password, globalOptions, callback) {
534
551
  try {
535
552
  const domain = ".facebook.com";
553
+ // Helper to extract userID from appState input
554
+ const extractUIDFromAppState = (appStateInput) => {
555
+ if (!appStateInput) return null;
556
+ let parsed = appStateInput;
557
+ if (typeof appStateInput === "string") {
558
+ try {
559
+ parsed = JSON.parse(appStateInput);
560
+ } catch {
561
+ return null;
562
+ }
563
+ }
564
+ if (Array.isArray(parsed)) {
565
+ const cUser = parsed.find(c => (c.key === "c_user" || c.name === "c_user"));
566
+ if (cUser) return cUser.value;
567
+ const iUser = parsed.find(c => (c.key === "i_user" || c.name === "i_user"));
568
+ if (iUser) return iUser.value;
569
+ }
570
+ return null;
571
+ };
572
+ let userIDFromAppState = extractUIDFromAppState(appState);
536
573
  try {
537
574
  if (appState) {
538
575
  if (typeof appState === "string") {
@@ -682,13 +719,26 @@ function loginHelper(appState, Cookie, email, password, globalOptions, callback)
682
719
  const processed = (await ctx.bypassAutomation(res, jar)) || res;
683
720
  let html = processed && processed.data ? processed.data : "";
684
721
  let cookies = await Promise.resolve(jar.getCookies("https://www.facebook.com"));
685
- let userID =
686
- cookies.find(c => c.key === "i_user")?.value ||
687
- cookies.find(c => c.key === "c_user")?.value ||
688
- cookies.find(c => c.name === "i_user")?.value ||
689
- cookies.find(c => c.name === "c_user")?.value;
722
+ const getUIDFromCookies = cs =>
723
+ cs.find(c => c.key === "i_user")?.value ||
724
+ cs.find(c => c.key === "c_user")?.value ||
725
+ cs.find(c => c.name === "i_user")?.value ||
726
+ cs.find(c => c.name === "c_user")?.value;
727
+ const getUIDFromHTML = body => {
728
+ const s = typeof body === "string" ? body : String(body ?? "");
729
+ return s.match(/"USER_ID"\s*:\s*"(\d+)"/)?.[1] || s.match(/\["CurrentUserInitialData",\[\],\{.*?"USER_ID":"(\d+)".*?\},\d+\]/)?.[1];
730
+ };
731
+ let userID = getUIDFromCookies(cookies);
732
+ // Also try to extract userID from HTML if not found in cookies
733
+ if (!userID) {
734
+ userID = getUIDFromHTML(html);
735
+ }
736
+ // If still not found and appState was provided, use userID from appState input as fallback
737
+ if (!userID && userIDFromAppState) {
738
+ userID = userIDFromAppState;
739
+ }
690
740
  if (!userID) {
691
- // Pass skipBackup=true if appState/Cookie was originally provided
741
+ // Pass hadAppStateInput=true if appState/Cookie was originally provided
692
742
  const retried = await tryAutoLoginIfNeeded(html, cookies, globalOptions, ctx, !!(appState || Cookie));
693
743
  html = retried.html;
694
744
  cookies = retried.cookies;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongdev/fca-unofficial",
3
- "version": "3.0.9",
3
+ "version": "3.0.10",
4
4
  "description": "Unofficial Facebook Chat API for Node.js - Interact with Facebook Messenger programmatically",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",