Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afterAuth not run when installing in Remix App #674

Open
NguyenQuangHuy282002 opened this issue Feb 28, 2024 · 7 comments
Open

afterAuth not run when installing in Remix App #674

NguyenQuangHuy282002 opened this issue Feb 28, 2024 · 7 comments

Comments

@NguyenQuangHuy282002
Copy link

NguyenQuangHuy282002 commented Feb 28, 2024

Issue summary

  • "@shopify/shopify-api": "^9.0.1",
    "@shopify/shopify-app-remix": "^2.3.0",
  • Node version: 20
  • Operating system: MACOS
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
Copy link

thorupio commented Mar 6, 2024

Also experiencing this

@thorupio
Copy link

thorupio commented Mar 6, 2024

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
Copy link

gnikyt commented Apr 3, 2024

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
Copy link

david1542 commented May 5, 2024

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
Copy link

NielXu commented Jun 8, 2024

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!

Copy link
Contributor

github-actions bot commented Aug 7, 2024

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.

@github-actions github-actions bot added the Stale label Aug 7, 2024
@erevol
Copy link

erevol commented Aug 29, 2024

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:

  • When the app is first installed, it generates a session record in the database.
  • However, this session is maintained only temporarily. It gets deleted when it tries to fetch the session to prevent duplication. This deletion only occurs when navigating to another route or refreshing the page.
  • To resolve this, I switched from using EventBridge for the app/uninstalled event to using the HTTP delivery method. This change allow me to properly handle session deletion when the app/uninstalled event is triggered.

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!

@github-actions github-actions bot removed the Stale label Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants