Slack Bolt – OAuth:InstallProvider:0 Error: The state parameter is not for this browser session

I’ve set up a Slack bot with Node.js and @slack/bolt. It uses socket mode.

To install the bot to a workspace, I go to https://localhost/slack/install, which has a link to Slack to install the bot. Slack then redirects to https://localhost/slack/oauth_redirect. That URL gives the following error in the server console:

[ERROR]  OAuth:InstallProvider:0 Error: The state parameter is not for this browser session.
    at new InvalidStateError (D:[email protected]:65:47)
    at InstallProvider.<anonymous> (D:CodingProjectsGearboxnode_modules@slackoauthdistinstall-provider.js:498:39)
    at step (D:CodingProjectsGearboxnode_modules@slackoauthdistinstall-provider.js:44:23)
    at Object.next (D:CodingProjectsGearboxnode_modules@slackoauthdistinstall-provider.js:25:53)
    at D:CodingProjectsGearboxnode_modules@slackoauthdistinstall-provider.js:19:71
    at new Promise (<anonymous>)
    at __awaiter (D:CodingProjectsGearboxnode_modules@slackoauthdistinstall-provider.js:15:12)
    at InstallProvider.handleCallback (D:CodingProjectsGearboxnode_modules@slackoauthdistinstall-provider.js:462:16)
    at Server.<anonymous> (D:CodingProjectsGearboxnode_modules@slackboltdistreceiversSocketModeReceiver.js:85:50)
    at Server.emit (node:events:518:28)
    at parserOnIncoming (node:_http_server:1143:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17) {
  code: 'slack_oauth_invalid_state'

I have verified that my environment variables are correct and have updated both @slack/bolt and @slack/oauth. I am piping the HTTP request from port 443 (where the rest of my website runs) to another SLACK_PORT.

My bot code is as follows:

import { SlashCommand, AckFn, RespondArguments, RespondFn, App, Installation } from "@slack/bolt";
import SlackCommands from "./SlackCommands";
import { Collections, getDatabase } from "./MongoDB";

const slackApp = new App({
  // token: process.env.SLACK_BOT_TOKEN,
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  socketMode: true,
  port: +process.env.SLACK_PORT,
  redirectUri: "https://localhost/slack/oauth_redirect",
  installerOptions: {
    redirectUriPath: "/slack/oauth_redirect",
  },
  appToken: process.env.SLACK_APP_TOKEN,
  clientId: process.env.NEXT_PUBLIC_SLACK_CLIENT_ID,
  clientSecret: process.env.SLACK_CLIENT_SECRET,
  stateSecret: process.env.SLACK_STATE_SECRET,
  scopes: ["chat:write", "commands"],
  installationStore: {
    storeInstallation: async (installation) => {
      // ... code to store the installation
    },
    fetchInstallation: async (InstallQuery) => {
      // ... code to fetch the installation
    },
  },
});

export async function startSlackApp() {
    await slackApp.start(process.env.SLACK_PORT);
    console.log("Slack bot is listening at: http://localhost:" + process.env.SLACK_PORT);

    return slackApp;
}

How do I set up the installation so the state parameter is valid?