From 2c59eb66357c4a5931d11189262078ecec552a46 Mon Sep 17 00:00:00 2001 From: Prasoon Shukla Date: Sat, 12 Aug 2023 14:09:52 +0530 Subject: [PATCH 1/4] feat: add authenticator method and rm authenticationEndpoint --- client/vanillajs/index.js | 47 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/client/vanillajs/index.js b/client/vanillajs/index.js index d988f5c..6a46046 100644 --- a/client/vanillajs/index.js +++ b/client/vanillajs/index.js @@ -12,6 +12,47 @@ import '@uppy/webcam/dist/style.css' const SERVER_BASE_URL = window.SERVER_BASE_URL; const IMAGEKIT_PUBLIC_KEY = window.IMAGEKIT_PUBLIC_KEY; +const AUTHENTICATOR_TIMEOUT = 6000; + +const authenticator = () => { + return new Promise((resolve, reject) => { + var xhr = new XMLHttpRequest(); + xhr.timeout = AUTHENTICATOR_TIMEOUT; + var url = `${SERVER_BASE_URL}/auth`; + if (url.indexOf("?") === -1) { + url += `?t=${Math.random().toString()}`; + } else { + url += `&t=${Math.random().toString()}`; + } + xhr.open('GET', url); + xhr.ontimeout = function (e) { + reject(["Authentication request timed out in 60 seconds", xhr]); + }; + xhr.addEventListener("load", () => { + if (xhr.status === 200) { + try { + var body = JSON.parse(xhr.responseText); + var obj = { + signature: body.signature, + expire: body.expire, + token: body.token + } + resolve(obj); + } catch (ex) { + reject([ex, xhr]); + } + } else { + try { + var error = JSON.parse(xhr.responseText); + reject([error, xhr]); + } catch (ex) { + reject([ex, xhr]); + } + } + }); + xhr.send(); + }) +} const metaFields = [ { @@ -61,8 +102,8 @@ const uppy = Uppy({ debug: true, autoProceed: false }) inline: true, trigger: '#uppyDashboard', metaFields: metaFields, - proudlyDisplayPoweredByUppy : false, - note : "ImageKit Uppy Sample • https://github.com/imagekit-samples/uppy-uploader" + proudlyDisplayPoweredByUppy: false, + note: "ImageKit Uppy Sample • https://github.com/imagekit-samples/uppy-uploader" }) .use(GoogleDrive, { target: Dashboard, companionUrl: SERVER_BASE_URL }) // don't add trailing slash .use(Dropbox, { target: Dashboard, companionUrl: SERVER_BASE_URL }) @@ -71,8 +112,8 @@ const uppy = Uppy({ debug: true, autoProceed: false }) .use(Url, { target: Dashboard, companionUrl: SERVER_BASE_URL }) .use(ImageKitUppyPlugin, { id: 'ImageKit', - authenticationEndpoint: `${SERVER_BASE_URL}/auth`, publicKey: IMAGEKIT_PUBLIC_KEY, + authenticator, metaFields: [ "useUniqueFileName", "tags", From 4ffdf5d5eb90a18f6f8cdb6410d99ae4f0e45cf2 Mon Sep 17 00:00:00 2001 From: Prasoon Shukla Date: Sat, 12 Aug 2023 14:13:52 +0530 Subject: [PATCH 2/4] feat: imagekit-uppy-plugin version change --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2f7b702..1deb621 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "express": "^4.17.1", "express-session": "^1.17.1", "imagekit": "^3.0.5", - "imagekit-uppy-plugin": "1.0.0-beta.1", + "imagekit-uppy-plugin": "^2.0.0", "path": "^0.12.7" }, "devDependencies": { From 83974942728c280870d4831bd25493d9ba00144b Mon Sep 17 00:00:00 2001 From: Prasoon Shukla Date: Thu, 24 Aug 2023 12:58:03 +0530 Subject: [PATCH 3/4] fix: use new js syntax for authenticator fn --- client/vanillajs/index.js | 58 +++++++++++++-------------------------- package.json | 2 +- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/client/vanillajs/index.js b/client/vanillajs/index.js index 6a46046..1856555 100644 --- a/client/vanillajs/index.js +++ b/client/vanillajs/index.js @@ -1,3 +1,5 @@ +import 'regenerator-runtime/runtime' + import Uppy from '@uppy/core' import '@uppy/core/dist/style.css' import '@uppy/dashboard/dist/style.css' @@ -12,47 +14,25 @@ import '@uppy/webcam/dist/style.css' const SERVER_BASE_URL = window.SERVER_BASE_URL; const IMAGEKIT_PUBLIC_KEY = window.IMAGEKIT_PUBLIC_KEY; -const AUTHENTICATOR_TIMEOUT = 6000; -const authenticator = () => { - return new Promise((resolve, reject) => { - var xhr = new XMLHttpRequest(); - xhr.timeout = AUTHENTICATOR_TIMEOUT; - var url = `${SERVER_BASE_URL}/auth`; - if (url.indexOf("?") === -1) { - url += `?t=${Math.random().toString()}`; - } else { - url += `&t=${Math.random().toString()}`; +const authenticator = async () => { + try { + + // You can pass headers as well and later validate the request source in the backend, or you can use headers for any other use case. + const response = await fetch(`${SERVER_BASE_URL}/auth`); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error(`Request failed with status ${response.status}: ${errorText}`); } - xhr.open('GET', url); - xhr.ontimeout = function (e) { - reject(["Authentication request timed out in 60 seconds", xhr]); - }; - xhr.addEventListener("load", () => { - if (xhr.status === 200) { - try { - var body = JSON.parse(xhr.responseText); - var obj = { - signature: body.signature, - expire: body.expire, - token: body.token - } - resolve(obj); - } catch (ex) { - reject([ex, xhr]); - } - } else { - try { - var error = JSON.parse(xhr.responseText); - reject([error, xhr]); - } catch (ex) { - reject([ex, xhr]); - } - } - }); - xhr.send(); - }) -} + + const data = await response.json(); + const { signature, expire, token } = data; + return { signature, expire, token }; + } catch (error) { + throw new Error(`Authentication request failed: ${error.message}`); + } +}; const metaFields = [ { diff --git a/package.json b/package.json index 1deb621..2f7b702 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "express": "^4.17.1", "express-session": "^1.17.1", "imagekit": "^3.0.5", - "imagekit-uppy-plugin": "^2.0.0", + "imagekit-uppy-plugin": "1.0.0-beta.1", "path": "^0.12.7" }, "devDependencies": { From 3ac14a1c54e2a86be7a85e07e035c8d1d7d07ba6 Mon Sep 17 00:00:00 2001 From: Prasoon Shukla Date: Thu, 24 Aug 2023 13:10:31 +0530 Subject: [PATCH 4/4] revert: imagekit-uppy-plugin version to 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2f7b702..1deb621 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "express": "^4.17.1", "express-session": "^1.17.1", "imagekit": "^3.0.5", - "imagekit-uppy-plugin": "1.0.0-beta.1", + "imagekit-uppy-plugin": "^2.0.0", "path": "^0.12.7" }, "devDependencies": {