Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return async function verifyTokenMiddleware(
ctx: Context,
next: NextFunction,
) {
const {session} = ctx;
if (session && session.accessToken) {
ctx.cookies.set(TOP_LEVEL_OAUTH_COOKIE_NAME);
// If a user has installed the store previously on their shop, the accessToken can be stored in session.
// we need to check if the accessToken is valid, and the only way to do this is by hitting the api.
const response = await fetch(
`https://${session.shop}/admin/metafields.json`,
{
method: Method.Post,
headers: {
[Header.ContentType]: 'application/json',
'X-Shopify-Access-Token': session.accessToken,
},
},
);
if (response.status === StatusCode.Unauthorized) {
redirectToAuth(routes, ctx);
return;
}
await next();
return;
}
export async function registerWebhook({
address,
topic,
accessToken,
shop,
apiVersion,
}: Options) {
const response = await fetch(
`https://${shop}/admin/api/${apiVersion}/graphql.json`,
{
method: Method.Post,
body: buildQuery(topic, address),
headers: {
[WebhookHeader.AccessToken]: accessToken,
[Header.ContentType]: 'application/graphql',
},
},
);
const result = await response.json();
if (
result.data &&
result.data.webhookSubscriptionCreate &&
result.data.webhookSubscriptionCreate.webhookSubscription
) {
return {success: true, result};
timeout.current = setTimeout(async () => {
if (timeout.current) {
clearTimeout(timeout.current);
timeout.current = undefined;
}
try {
await fetch(url, {
method: Method.Post,
headers: {
[Header.ContentType]: 'application/json',
},
body: JSON.stringify({
connection: serializableClone((navigator as any).connection),
events: events.current,
navigations: navigations.current.map(navigation => ({
details: navigation.toJSON({removeEventMetadata: false}),
metadata: navigation.metadata,
})),
pathname: window.location.pathname,
}),
});
} catch (error) {
if (onError) {
onError(error);