0

again I'm following the directions here at https://github.com/Shopify/shopify-node-api/blob/main/docs/getting_started.md to try to spin up a super simple app.

When I hit localhost:3000 I'm getting a too many redirects error.

The code is below:

// src/index.ts
import http from 'http';
import url from 'url';
import querystring from 'querystring';
import Shopify, { ApiVersion, AuthQuery } from '@shopify/shopify-api';
require('dotenv').config();

const { API_KEY, API_SECRET_KEY, SCOPES, SHOP, HOST } = process.env

Shopify.Context.initialize({
  API_KEY,
  API_SECRET_KEY,
  SCOPES: [SCOPES],
  HOST_NAME: HOST.replace(/https?:\/\//, ""),
  HOST_SCHEME: HOST.split("://")[0],
  IS_EMBEDDED_APP: true,
  API_VERSION: ApiVersion.October21 // all supported versions are available, as well as "unstable" and "unversioned"
});

// Storing the currently active shops in memory will force them to re-login when your server restarts. You should
// persist this object in your app.
const ACTIVE_SHOPIFY_SHOPS: { [key: string]: string | undefined } = {};

async function onRequest(
    request: http.IncomingMessage,
    response: http.ServerResponse,
  ): Promise<void> {
    const {headers, url: req_url} = request;
    const pathName: string | null = url.parse(req_url).pathname;
    const queryString: string = String(url.parse(req_url).query);
    const query: Record<string, any> = querystring.parse(queryString);
  
    switch (pathName) {
      default:
        // This shop hasn't been seen yet, go through OAuth to create a session
        if (ACTIVE_SHOPIFY_SHOPS[SHOP] === undefined) {
          // not logged in, redirect to login
          response.writeHead(302, {Location: `/login`});
          response.end();
        } else {
          response.write('Hello world!');
          // Load your app skeleton page with App Bridge, and do something amazing!
        }
        return;
    } // end of default path
  } // end of onRequest()
  
  http.createServer(onRequest).listen(3000);

I'm not sure what to do or how to debug this, I looked up too many redirects problems on here but they dont fit in this context

The localhost page isn’t working. localhost redirected you too many times

This page isn’t working localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

HOw do I fix this in the context of the shopify api?

kawnah
  • 3,204
  • 8
  • 53
  • 103
  • I haven't used the new setup but if I remember correctly you shouldn't access the localhost url, but the ngrok one. So instead of `localhost:3000` you should go to `UNIQUE_STRING.ngrok.io` – drip Jun 07 '22 at 06:22
  • I tried that but I still get too many redirects – kawnah Jun 07 '22 at 15:30

0 Answers0