Encrypt and Decrypt Data with return to client side

Dear All,

I am working on some code at present the reason for the encryption is that I have a page open to access by anyone and do not want malitious visitors to be able to read others details should they somehow gain access to the data collections. I have created a backend module called decrypt.jsw and this is the code for decrypting the cipher of the email, I am wanting to return the data to client side once it has been decrypted but on client side code I am receiving an undefined response?

backend code:

export function decryptEmailOne() {
 return decryptEmail();
}

function decryptEmail() {
 return wixData.get("VeriTagsProfileData", wixUsers.currentUser.id)
        .then((results) => {
 let item = results;
 let email = item.email
            console.log(email); // Logs the encrypted email 
            generateKey().then(function (code) {
                console.log(code); // Logs the decryption key 
 let key = code;
 let ciphertext = email;
 // Decrypt
 let bytes = CryptoJS.AES.decrypt(ciphertext, 'key');
 let originalText = bytes.toString(CryptoJS.enc.Utf8);
                console.log(originalText); // this logs the correct details
 var data = originalText;
                console.log(data); // this logs the correct details//
 return data; // there seems to be an issue with the return
            })
        })
}

client side code:


import { decryptEmailOne } from 'backend/decrypt.jsw';

export function button85_click(event) {
    console.log("call to function"); // logs the action
    decryptEmailOne().then(function(data) {
        console.log(data); // logs undefined
    })
}

Could someone help out with a little bit of code manipulation so that I get the decrypted result return to client side code?

@brainstorrrm @Yisrael (Wix) @Alexander (Wix)

any help would be greatly appreciated.

Best wishes

Si

Just a superficial observation: the line of code that begins:
generateKey().then( function (code) {

should read:
generateKey().then( function (code) => {

I hope this helps.

Actually, fat arrows signify a function, so you either use:
function() { }
or () => { }.

Not both!

So first, try:

import { decryptEmailOne } from 'backend/decrypt.jsw';

export async function button85_click(event) {
    console.log("call to function"); // logs the action
    let result = await decryptEmailOne();
    console.log(result);
    //decryptEmailOne().then(function(data) {
// your function is not a promise, so nothing will happen here and data //is undefined as a result
//.then() only exists on promises
    //})
}

But also, in your backend function, you’re reassigning a lot of variables unnecessarily:

let item = results;
let key = code;
var data = originalText;

It won’t affect the result, but it’s just repetitive/less readable.

I am having some issues with the above code. These issues are probably because I am coding for the first time and do not know what I’m doing. I am also modifying this code to encrypt content in a particular input field, if that affects any resolutions.
Errors:
‘generateKey’ is not defined
‘CryptoJS’ is not defined
How would I go about resolving this?

@apologetics2222 Hello, To get the best support from the community, please create a new post with the specific details of your problem (not just that you are a new coder! :grinning:), including a description, relevant code, and any screenshots. The posting guidelines are here . Thank you!