Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

388
Views
how to pass Auth Token from login window to app window in electron

I created an app in electron with react. I made two windows: login window and app window. In the login window, I set up a basic HTML form and I call the login endpoint from being like this:

async function login() {
  const response = await loginResponse(
    usernameInput.value,
    passwordInput.value
  );
  if (response?.invalidData === true) {
    alert("Wrong credentials!");
    return;
  }
  return ipcRenderer.send("open-app", response?.token, response?.displayName);
}

The event open-app it's for creating an app window and sending a token, displayName to react app:

ipcMain.on("open-app", async (e, token, displayName) => {
    console.log(token, displayName);
    if (appWindow) {
      appWindow.focus();
      return;
    }
    appWindow = new BrowserWindow({
      width: 1200,
      height: 900,
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
        enableRemoteModule: true,
      },
    });

    appWindow.webContents.on("dom-ready", (e) => {
      appWindow.webContents.send("auth-data", {
        token,
        displayName,
      });
    });

    appWindow.loadURL(
      isDev
        ? "http://localhost:3000"
        : `file://${__dirname}/../build/index.html`
    );
});

And in my App.js I save token in local storage:

useEffect(() => {
    ipcRenderer.on("auth-data", async (data) => {
      console.log(data);
      const { token, displayName } = data;
      console.log(token);
      if (token && displayName) {
        window.localStorage.setItem("auth", token);
        window.localStorage.setItem("displayName", displayName);
        await apiService.markOnline(token);
      }
    });
  }, []);

the problem it's the token doesn't save or if it's saved it doesn't update in localstorage when I re-open the app. How to send and update properly the token between the windows?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!