Getting Store Credentials Post-OAuth in Shopify App
I built a Shopify application using the remix framework. After a store owner approves the app permissions during installation, I need to capture the access token and store URL to save them in AWS Secrets Manager.
The rest of my application works fine. I just need to automatically update theme files, which requires the access token to work properly.
I found what seems like the right file for this:
import type { LoaderFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";
export const loader = async ({ request }: LoaderFunctionArgs) => {
await authenticate.admin(request);
// Store Token > AWS Operations
return null;
};
The issue is I cannot retrieve the shop identifier. I am not even sure if that is necessary, but converting the session token to an access token seems to require it.
I want to modify this function to grab both the access token and shop name, then send them to AWS during admin authentication.
My Prisma database schema includes the access token:
model Session {
id String @id
shop String
state String
isOnline Boolean @default(false)
scope String?
expires DateTime?
accessToken String
userId BigInt?
firstName String?
lastName String?
email String?
accountOwner Boolean @default(false)
locale String?
collaborator Boolean? @default(false)
emailVerified Boolean? @default(false)
}
So waiting for the authentication should populate the session data above. Then I should be able to access the token, right?
I tried this approach based on some tutorials, but it does not work:
import type { LoaderFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export const loader = async ({ request }: LoaderFunctionArgs) => {
const sessionData = await authenticate.admin(request);
console.log("Session data:", sessionData);
const shopName = sessionData.input_data.shop.domain;
const storeToken = await fetchTokenForStore(shopName);
console.log("Store token:", storeToken);
return null;
};
async function fetchTokenForStore(shopName: string): Promise<string | null> {
const sessionRecord = await prisma.session.findUnique({
where: {
shop: shopName,
},
});
return sessionRecord ? sessionRecord.accessToken : null;
}
The shop variable I got from a tutorial about getting shop names from client-side checkout sessions. I doubt it works the same way for admin sessions. Any guidance would be helpful.