I am building an API that requires users to login in a website and I'm handling this with Puppeteer.
The problem here is when two users using the API at the same time (Or in the same browser session), I get an error saying that the user is already logged in and I think this happened because it is the same session.
Here is my current code:
import express from "express";
import puppeteer from "puppeteer";
const myRoutes = express.Router();
myRoutes.get("/login", async (request, response) => {
const loginAuth = request.body;
try {
const browser = await MyBrowser.initialize();
const page = await browser.newPage();
await page.goto("https://website.com/login");
await page.waitForNetworkIdle({ idleTime: 3000 });
await page.$eval("#userName", (el, value) => (el.value = value), loginAuth.userName);
await page.$eval("#password", (el, value) => (el.value = value), loginAuth.password);
await page.$eval("#loginBtn", (el) => el.click());
await page.$eval("#loginBtn", (el) => el.click());
// MyBrowser.closeBrowser();
} catch (err) {
console.log("ERRORR OCCURED...");
console.log(err);
}
response.end("No message");
});
class MyBrowser {
static #browser;
static async initialize() {
if (!MyBrowser.#browser?.isConnected())
MyBrowser.#browser = await puppeteer.launch({ args: ["--no-sandbox"] });
}
static async closeBrowser() {
if (MyBrowser.#browser?.isConnected()) await MyBrowser.#browser.close();
}
}
export { myRoutes };
when I send 2 requests at the same time, I get the following error:
Error: Error: failed to find element matching selector "#userName" at ElementHandle.$eval
This error is occurring because the first request user has already logged in and the element is no longer present on the page for the second request.
So I want to make sure that if two or more users using the API, they each will have their own session.
I know that I could use another puppeteer.launch()
but it may require a large amount of memory and I want to avoid that...
I have also tried to close the browser using MyBrowser.closeBrowser()
before the end of the request, but I encountered this error when I tried to send two requests at the same time:
Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed. at CDPSessionImpl.send
It works fine if I wait some times before each request.