• Creates and displays a browser window for OAuth authentication

    Creates a modal browser window for the Spotify OAuth flow with:

    • Proper parent-child relationship with the main application window
    • Secure web configuration (no node integration, context isolation)
    • Automatic event handlers for window lifecycle events
    • Appropriate size and styling for authentication forms
    • Modal behavior to ensure authentication focus

    The window is created hidden first, then shown once content has loaded to provide a smoother user experience. Event handlers manage the complete window lifecycle, including error scenarios and cleanup.

    Parameters

    • parentWindow: BrowserWindow

      Parent application window that owns this authentication dialog

    • url: string

      Spotify authorization URL to load in the window

    • onClose: () => void

      Callback function to execute when window is closed

    Returns BrowserWindow

    The created BrowserWindow instance for further customization if needed

    // Create authentication window from main application window
    const authWindow = createAuthWindow(
    mainWindow,
    'https://accounts.spotify.com/authorize?client_id=...',
    () => console.log('Auth window was closed')
    );
    export function createAuthWindow(
    parentWindow: BrowserWindow,
    url: string,
    onClose: () => void,
    ): BrowserWindow {
    // Clean up any existing window
    if (authWindow) {
    closeAuthWindow();
    }

    // Create a new authentication window
    authWindow = new BrowserWindow({
    parent: parentWindow,
    modal: true,
    width: 500,
    height: 700,
    show: false,
    webPreferences: {
    nodeIntegration: false,
    contextIsolation: true,
    },
    });

    // Configure window properties
    authWindow.setMenu(null);
    authWindow.setTitle("Spotify Authentication");

    // Set up event handlers
    authWindow.once("ready-to-show", () => {
    if (authWindow) {
    authWindow.show();
    saveLog("OAuth authentication window displayed", "DEBUG");
    }
    });

    authWindow.webContents.on(
    "did-fail-load",
    (_, errorCode, errorDescription) => {
    saveLog(
    `Authentication page failed to load: ${errorDescription} (${errorCode})`,
    "ERROR",
    );
    closeAuthWindow();
    onClose();
    },
    );

    authWindow.on("closed", () => {
    authWindow = null;
    onClose();
    });

    // Load the authorization URL
    authWindow.loadURL(url);
    saveLog(`OAuth window created and loading URL: ${url}`, "DEBUG");

    return authWindow;
    }