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

Solved
Indeed
Excursionist
13 1 2

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("/");
        },
      }),
  );

 

CalD
Shopify Staff
Shopify Staff
139 20 28

This is an accepted solution.

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. 

CalD | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Indeed
Excursionist
13 1 2

@CalD awesome, thank you!

0 Likes
ulybu
New Member
1 0 0

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!

0 Likes
WinkyApps
Explorer
54 13 12

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

Did my answer work for you? Please drop a Like and Mark it as Accepted.
If you need further help or want to get something custom built for your Shopify store, then feel free to mail our team on winkyapps18@gmail.com
Happy to help.
0 Likes