-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathshopify_app.js
68 lines (56 loc) · 2.21 KB
/
shopify_app.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Turbo } from "@hotwired/turbo-rails";
import { getSessionToken } from "@shopify/app-bridge/utilities";
import createApp from '@shopify/app-bridge';
import { setupRedirectHandler } from './navigation'
document.addEventListener('DOMContentLoaded', () => {
const shopifyAppInit = document.getElementById('shopify-app-init')
if (!shopifyAppInit) { return }
var data = shopifyAppInit.dataset;
if (data.forceIframe === "false") { return }
window.app = createApp({
apiKey: data.apiKey,
host: data.host,
forceRedirect: true,
});
// Append Shopify's JWT to every Turbo request
document.addEventListener('turbo:before-fetch-request', async (event) => {
event.preventDefault()
window.sessionToken = await retrieveToken();
event.detail.fetchOptions.headers['Authorization'] = `Bearer ${window.sessionToken}`
event.detail.resume()
})
// Redirect to the requested page
Turbo.visit(data.loadPath, { action: "replace" });
document.addEventListener("turbo:load", () => {
const shopifyAppInit = document.getElementById('shopify-app-init')
if (!shopifyAppInit) { return }
const data = shopifyAppInit.dataset;
const jwtExpireAt = data.jwtExpireAt;
if (window.sessionToken && jwtExpireAt) {
// Convert jwtExpireAt to milliseconds
window.jwtExpireAt = jwtExpireAt * 1000;
}
});
setupRedirectHandler()
});
export async function retrieveToken() {
if (window.sessionToken && window.jwtExpireAt && window.jwtExpireAt > Date.now()) {
const diff = parseInt((window.jwtExpireAt - Date.now()) / 1000) + 's';
console.log('[shopify_app] Reusing token. Expires in:', diff);
return window.sessionToken;
} else {
console.log('[shopify_app] Get new token');
return await getSessionToken(window.app);
}
}
// Force redirect via turbo using turbo_redirect_to helper in controller.
// Mandatory for Safari since it's loosing JWT token during 302 redirect.
document.addEventListener("turbo:before-fetch-response", (event) => {
const response = event.detail.fetchResponse;
const status = response.statusCode;
const location = response.header("Location");
if (status === 200 && location !== null) {
event.preventDefault();
Turbo.visit(location);
}
});