Karan Jaiswal
Karan Jaiswal

Reputation: 1

HubSpot OAuth tokens not received when user is not logged in, but works fine when already logged in

I am integrating HubSpot OAuth into my application. When the user is already logged in to HubSpot, the OAuth flow works perfectly, and I receive the tokens without any issues. However, when the user is not logged in to HubSpot and logs in during the OAuth flow, I do not receive the tokens as expected. Here's the code:

const tokenResponse = await axios.post('https://api.hubapi.com/oauth/v1/token', params.toString(), {
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
});

const tokens = { ...tokenResponse.data, code };
console.log('Received tokens:', tokens);

return response.status(200).send(
  `<html>
    <head><title>HubSpot Integration</title></head>
    <body>
      <h2>HubSpot App Installed Successfully!</h2>
      <p>You can close this window now.</p>
      <button onclick="window.close()">Close</button>
      <script>
        console.log(window.opener);
        if (window.opener) {
          window.opener.postMessage(${JSON.stringify(tokens)}, "http://localhost:3000/integrations");
        }
        console.log(window.location);
      </script>
    </body>
  </html>`
);

The issue occurs only when the user needs to log in to HubSpot before completing the OAuth flow. The flow redirects the user to HubSpot for authentication in a new window, but after logging in, I do not receive the tokens in my app. When the user is already logged in, everything works fine.

Upvotes: 0

Views: 32

Answers (0)

Related Questions