summaryrefslogtreecommitdiff
path: root/hooks/authenticatedFetch.js
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-18 21:34:33 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-18 21:34:33 +0800
commit5f40484088e509e49675813c34be3b53998514ea (patch)
tree2a8673652132ccdba2d75c1c3c63da16fe916cdc /hooks/authenticatedFetch.js
parent023d6464cbf37774ea93dd950bda9350179dfab0 (diff)
authenticatedFetch that doesn't use 'use'
Diffstat (limited to 'hooks/authenticatedFetch.js')
-rw-r--r--hooks/authenticatedFetch.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/hooks/authenticatedFetch.js b/hooks/authenticatedFetch.js
new file mode 100644
index 0000000..4b57ff7
--- /dev/null
+++ b/hooks/authenticatedFetch.js
@@ -0,0 +1,25 @@
+function authenticatedFetch(uri, options) {
+ var AppBridge = window['app-bridge'];
+ var app = AppBridge.createApp({
+ apiKey: '',
+ host: ''
+ });
+ var fetchFunction = window['app-bridge-utils'].authenticatedFetch(app);
+ return async (uri, options) => {
+ var response = await fetchFunction(uri, options);
+ var headers = response.headers;
+ if (headers.get("X-Shopify-API-Request-Failure-Reauthorize")
+ === "1") {
+ var authUrlHeader = headers.get(
+ "X-Shopify-API-Request-Failure-Reauthorize-Url")
+ || `/api/auth`;
+ var Redirect = AppBridge.actions.Redirect;
+ Redirect.create(app).dispatch(Redirect.Action.REMOTE,
+ authUrlHeader.startsWith("/")
+ ? `https://${window.location.host}${authUrlHeader}`
+ : authUrlHeader
+ );
+ }
+ return response;
+ };
+}