How to get associated user's account id in Koa afterAuth method

According to the Shopify OAuth documentation here, if online access mode is requested during the authentication flow, the associated user details are returned that will include the associated user’s id. We need this user id in our app in order to associate data with the specific user accessing the app.

We are authenticating the user by the OAuth flow as outlined in this tutorial. In our node.js server file, we are using createShopifyAuth and afterAuth methods to do this. There is the ability to set accessMode: “online” here, but I do not see a way to get the “associated_user” data that is supposed to be returned when using online mode with Shopify OAuth.

How can I get the user’s account id in the returned “associated_user” data in the afterAuth method?

Here is the portion of our server.js file for Shopify OAuth, and the comment here is where we are assuming we should be able to get the returned associated_user.id.

app.prepare().then(() => {
  const server = new Koa();
  server.use(session({ secure: true, sameSite: 'none' }, server));
  server.keys = [SHOPIFY_API_SECRET_KEY];
  server.use(
      createShopifyAuth({
        apiKey: SHOPIFY_API_KEY,
        secret: SHOPIFY_API_SECRET_KEY,
        scopes: ['read_products', 'write_products'],
        accessMode: "online",
        afterAuth(ctx) {
          const { shop, accessToken } = ctx.session;
          // How do we get associated_user.id returned in JSON response from Shopify OAuth request?
          ctx.cookies.set('shopOrigin', shop, {
            httpOnly: false,
            secure: true,
            sameSite: 'none'
          });
          ctx.redirect("/");
        },
      }),
  );
1 Like

Hey @Indeed ,

There’s a currently-open PR here: https://github.com/Shopify/quilt/pull/1542 to expose that info which you can track against a future koa-shopify-auth version that should allow you to access associated_user from the session object*.*

1 Like

@CalD awesome, thank you!

Hi @CalD ,

When performing the oauth “manually” and not with koa-shopify-auth (with passportjs for instance), I get a profile object that has quite many fields. Among those fields: displayName, emails, iana_timezone, currency, country_code, money_format, etc

  1. I don’t see those fields in the oauth documentation link from the OP. Am I mistaking things ?
  2. Does that mean that the future release of koa-shopify-auth will not contains those extra fields ?

Thanks!

Have the same question, is there a way to get shop default currency once oauth is success?