redshift
redshift

Reputation: 5227

NuxtJS: How to run NodejS code in a universal Nuxt app ? Trying to generate signature needed for imagekit upload request

I am a beginner to NuxtJS and backend development. I am trying to use the imagekit.io upload API so I can let my users publish images to a cloud storage service (Imagekit). In order to do so, I need to do "signature-based authentication" and implement the following NodeJS code somewhere in my Nuxt app. From what I understand, I believe Nuxt's /modules folder may be where it shall be placed because it is ran before Nuxt boots up...correct?

My client-side app requires the following example JSON response:

{
    token: "1bab386f-45ea-49e1-9f0d-6afe49a5b250",
    expire: 1580372696,
    signature: "0f9d5a45e97c24fa9200a9d5543c9af1e2c45a54"
}

Thus, the NodeJS code to calculate these parameters needs to be like so:

var ImageKit = require("imagekit");
var fs = require('fs');

var imagekit = new ImageKit({
    publicKey : "your_public_api_key",
    privateKey : "your_private_api_key",
    urlEndpoint : "https://ik.imagekit.io/your_imagekit_id/"
});

var authenticationParameters = imagekit.getAuthenticationParameters();
console.log(authenticationParameters);

How can I run the above NodeJS code in my universal Nuxt app so I can get the authenticationParameters needed? I also assume I need to create a nuxt hook to create the api endpoint? Apologies, I'm a bit lost.

Full documentation of what I am trying to do is here: https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload

Upvotes: 0

Views: 385

Answers (1)

kissu
kissu

Reputation: 46814

Modules is not the place for this, modules are aimed towards already done packages that are easy to use with Nuxt like https://image.nuxtjs.org/

Otherwise, most parts of Nuxt do run universally (on the backend then on the front-end). Since you're willing to use a private key, you will need to have it only on server and somehow pass the public token to your future calls.

Check the lifecycle for more info: https://nuxtjs.org/docs/concepts/nuxt-lifecycle#server

Upvotes: 1

Related Questions