This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
41 lines (34 loc) · 1.47 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { redirect } from "@remix-run/cloudflare";
function redirectToAuth(context, request) {
const { searchParams } = new URL(request.url);
if (!searchParams.get('shop')) {
throw new Response("No shop provided", { status: 500 });
}
if (searchParams.get("embedded") === "1") {
clientSideRedirect(context, request, "/oauth/install");
}
const queryParams = new URLSearchParams({ shop: searchParams.get('shop') });
throw redirect(`/oauth/install?${queryParams}`);
}
function clientSideRedirect(context, request, endpoint) {
const { searchParams } = new URL(request.url);
const { shopify } = context;
const shop = shopify.utils.sanitizeShop(searchParams.get('shop'));
const host = shopify.utils.sanitizeHost(searchParams.get('host'));
const redirectUriParams = new URLSearchParams({ shop, host }).toString();
const queryParams = new URLSearchParams({
shop,
host,
redirectUri: `https://${shopify.config.hostName}${endpoint}?${redirectUriParams}`,
}).toString();
throw redirect(`/exitiframe?${queryParams}`);
}
async function validateRequest(context, request) {
const { searchParams } = new URL(request.url);
const { shopify } = context;
const isValid = await shopify.utils.validateHmac(Object.fromEntries(searchParams.entries()));
if (!isValid) {
throw new Response('Could not verify request was from Shopify.', { status: 403 })
}
}
export { validateRequest, redirectToAuth }