/    Sign up×
Bounties /Pin to ProfileBookmark

Protected js code (im honestly not sure how to phrase this)

I have some javascript code that will determine if a code entered into an input will match any codes in a list. (It’s a discount code input) 

discountButton.addEventListener('click', () => {
  if (discountInput.value === 'hellogoodsir') {
    discountArea.style.outline = 'var(--main-color) 2px solid';
  } else {
    discountArea.style.outline = 'red 2px solid';
  };
})

right now instead of it being a list, the code will have to be the same as hellogoodsir, but if you click inspect, and into the code files you can see the code you need to input. The list of codes will be different codes set by the user of the document, and I need the list to be protected so someone has to get a discount code their own way (through promotions etc) Any way to do this?

to post a answer
Back-endFront-endJavaScript

2 Replies

Davinci•003 auto-replyYes, there is a way to do this, although it will involve more than just JavaScript on the client side. To protect sensitive data like a list of valid discount codes, you should use a server-side language (like Python, Node.js, PHP, etc.) to check if the discount code is valid.

Your server will need an endpoint (like '/check-discount-code') which accepts the discount code as a parameter and returns a response indicating whether the discount code is valid or not.

The server should keep a list of valid discount codes in a database or other secure storage, and check the inputted discount code against this list.

Here is an example of how you could modify your JavaScript to make a request to the server:

discountButton.addEventListener('click', () => {
let discountCode = discountInput.value;
fetch('/check-discount-code', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({code: discountCode}),
})
.then(response => response.json())
.then(data => {
if (data.isValid) {
discountArea.style.outline = 'var(--main-color) 2px solid';
} else {
discountArea.style.outline = 'red 2px solid';
}
})
.catch((error) => {
console.error('Error:', error);
});
});


This code uses the fetch API to send a POST request to the server. It sends the entered discount code in the body of the request. The server checks the code and returns a JSON object with a property 'isValid' indicating whether the code is valid or not.

On the server side, you could have something like this (in express.js for example):

app.post('/check-discount-code', (req, res) => {
let discountCode = req.body.code;
// Assuming 'validCodes' is your list of valid discount codes
if (validCodes.includes(discountCode)) {
res.json({ isValid: true });
} else {
res.json({ isValid: false });
}
});


Please note that in a real world application, you'll need to secure this endpoint and potentially rate limit it to prevent brute force attacks, and always ensure proper error handling is in place.

Was this helpful?

Copy linkTweet thisAlerts:
@themolitorJul 24.2023 — Updated Davinci•003 auto-reply 👆
@ProductionNerdauthorIt makes sense, thank you, but do you have any idea on how I can connect this to my js file and if I need to create any more files? It doesn't specify. Thank youJul 25.2023
×

Success!

Help @ProductionNerd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...