Shopify / shopify-app-js

MIT License
262 stars 101 forks source link

afterAuth not run when installing in Remix App #674

Open NguyenQuangHuy282002 opened 6 months ago

NguyenQuangHuy282002 commented 6 months ago

Issue summary

const shopify = shopifyApp({
  apiKey: process.env.SHOPIFY_API_KEY,
  apiSecretKey: process.env.SHOPIFY_API_SECRET || '',
  apiVersion: LATEST_API_VERSION,
  scopes: process.env.SCOPES?.split(','),
  appUrl: process.env.SHOPIFY_APP_URL || '',
  authPathPrefix: '/auth',
  sessionStorage: new MongoDBSessionStorage(new URL(process.env.DATABASE_URL || ''), ''),
  // sessionStorage: new PrismaSessionStorage(prisma),
  distribution: AppDistribution.AppStore,
  restResources,
  webhooks: {
    [APP_UNINSTALLED]: {
      ...DEFAULT_WEBHOOK_METHOD,
    },
    [CUSTOMERS_DATA_REQUEST]: {
      ...DEFAULT_WEBHOOK_METHOD,
    },
    [CUSTOMERS_REDACT]: {
      ...DEFAULT_WEBHOOK_METHOD,
    },
    [SHOP_REDACT]: {
      ...DEFAULT_WEBHOOK_METHOD,
    },
    [SHOP_UPDATE]: {
      ...DEFAULT_WEBHOOK_METHOD,
    },
  },
  hooks: {
    afterAuth: async ({ session, admin }) => {
console.log('Run after auth hook')
      shopify.registerWebhooks({ session })

      await Promise.all([initShopData(admin, session), installApp(prisma, admin.graphql, session)])
    },
  },
  future: {
    v3_webhookAdminContext: true,
    v3_authenticatePublic: true,
  },
  ...(process.env.SHOP_CUSTOM_DOMAIN ? { customShopDomains: [process.env.SHOP_CUSTOM_DOMAIN] } : {}),
})

Expected behavior

Run afterAuth callback after installed

Actual behavior

Not run afterAuth callback after installed, however, run it when navigating to other routes in app

Steps to reproduce the problem

  1. Install app
  2. Check the afterAuth callback is run by logging in terminal
thorupio commented 6 months ago

Also experiencing this

thorupio commented 6 months ago

fwiw, I'm seeing this being logged if I set logger: { level: LogSeverity.Debug }

[shopify-app/DEBUG] App is not embedded, redirecting to Shopify | {shop:{redacted}.myshopify.com}

But the app IS embedded

gnikyt commented 5 months ago

I ran across this issue, this whole product is just unfinished and buggy unfortunately. Every app we develop, we run into issues directly related to the framework Shopify is suggesting to use... super frustrating.

In this case, I decided to spin up a new app just to see if that would resolve the issue, via npm run dev -- --reset and going through the steps to setup a new app.

I then went onto the existing app's configuration page in the Partners portal, and compared it to the new app that was just setup. I noticed the old app had no scopes section in the partners page, the new app did. So something has been changed along the way, unsure what.

However, creating a new app did resolve the issue.

Additionally, the root issue appears to be the installation URL provided under Distribution, did not end up containing a ?embedded=1 URL param as part of the install/redirects, which caused it to think it was not embedded (node_modules/@shopify/shopify-app-remix/build/cjs/server/authenticate/admin/helpers/ensure-app-is-embedded-if-required.js -- line 15).

So, try creating a new app.

david1542 commented 4 months ago

I'm also experiencing this issue :( I've just created a new application and the afterAuth is not fired. I'm not sure why.

EDIT: ok turns out I had to delete my local sqlite db and delete the session. Afterwards, the afterAuth was run correctly.

NielXu commented 3 months ago

I'm also experiencing this issue :( I've just created a new application and the afterAuth is not fired. I'm not sure why.

EDIT: ok turns out I had to delete my local sqlite db and delete the session. Afterwards, the afterAuth was run correctly.

This saved my day!

github-actions[bot] commented 1 month ago

We're labeling this issue as stale because there hasn't been any activity on it for 60 days. While the issue will stay open and we hope to resolve it, this helps us prioritize community requests.

You can add a comment to remove the label if it's still relevant, and we can re-evaluate it.

erevol commented 2 weeks ago

Hi everyone,

I encountered the same issue where the afterAuth callback wasn't running when installing the app. In my case, the problem was related to session saved in the db. Since I was using EventBridge instead of HTTP delivery method, I forgot to delete the session from the db once the shop uninstalls the app, causing this issue.

Here's what I found:

webhooks.jsx

switch (topic) {
    case "APP_UNINSTALLED":
      if (session) {
        await db.session.deleteMany({ where: { shop } }); // This line removes the session from db
      }
      break;

After making this change, the afterAuth callback ran as expected after the app was installed.

I hope this helps others facing the same issue!