diff --git a/client/vanillajs/index.js b/client/vanillajs/index.js index d988f5c..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' @@ -13,6 +15,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 = 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}`); + } + + 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 = [ { id: 'name', name: 'File name', placeholder: 'Enter the file name' @@ -61,8 +82,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 +92,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", 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": {