Hi there,
i do an advent calendar on my customers website.
There is a database and every day there is another discount available.
i use setfilter to display today’s offer.
to see ALL offers you will have to register to the members area.
my problem:
anyone could just change the computers date to see tomorrow’s offer.
how can i prevent this on serverside?
greetings
Hey there!
You will need to create a backend module that will return the date of the server.
-
Create a new Backend Web Module and name it dateServices.jsw for example.
-
Add the below code into that file after cleaning it from sample code.
// Filename: backend/dateServices.jsw (web modules need to have a .jsw extension)
export function getServerDate() {
return new Date().toDateString();
}
- In the page where you need the date add an import statement on top of the page like below
import {getServerDate} from 'backend/dateServices';
- Then await call that function to get the date from server side.
let myDate = await getServerDate();
- Or wait for the response like below
getServerDate().then((myDate) => {
console.log(myDate);
})
In the server side module I use toDateString() but here you will need to use the thing you want. If you just want the day number get that back and so on.
Good luck! If this post was the answer please mark it as Top Comment as it will help others to find it.
Looks great.
But i guess one could still open the chrome developer console and activate another setfilter command, right?
@der-barde Not if you get your data from a backend module as well, then data is rendered server side and they won’t be able to modify it.
first of all thanks for the hint!
it seems i have to wait until the server request is fulfilled( they call it promise?)
but when using await it tells this cannot be used since my function is not async.
i guess this does not suffice?
$w.onReady(function () {
let today = getServerDate();
and, can i just use
return new Date();