Help authenticating using spotify API (oauth2.0 flow)

I have a React app in Electron, and I’m trying to access the spotify API using the spotify-web-api-node library. However, I’m not sure exactly how the oauth flow is meant to work inside of an Electron app… Firstly, for the redirect URL, I added a registerFileProtocol call to my file. Then I added a specific ipcMain.on handler for receiving the spotify login call from a page. I then open the native browser with shell.openExternal(authorizeURL). This all works.

The part that doesn’t work is the callback; when the spotify auth page succeeds and tries to call oauthdesktop://test as the callback, it opens my app again (i.e. I get new open windows) but it does NOT call the handler I registered in registerFileProtocol. How do I get it to do that? All I need is the token that it’s responding with…

This is part of my main.js:

app.whenReady().then(() => {
    (request, callback) => {
      console.log("oauthdesktop stuff: ", request, callback);
      //parse authorization code from request
    (error) => {
      if (error) console.error("Failed to register protocol");

ipcMain.on("spotify-login", (e, arg) => {
  const credentials = {
    clientId: arg.spotifyClientId,
    clientSecret: arg.spotifySecret,
    redirectUri: "oauthdesktop://test",

  const spotifyApi = new SpotifyWebApi(credentials);
  console.log("spapi: ", spotifyApi);

  const authorizeURL = spotifyApi.createAuthorizeURL(
    ["user-read-recently-played", "playlist-modify-private"],
  console.log("spurl: ", authorizeURL);