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