0

In my flutter application I want to prevent users to enter their nominations after the cutoff date.

For example, there is an exam nomination with cutoff date 31st March 2020. I am inserting the nomination data from my flutter app to the Firebase database. In app I'm checking the current date should be before the 31st March and then only allow to enter the data. However if the user changes the mobile data to backward the the app will allow to enter the nominations for the exam.

I know if I write the Firebase Cloud Function and move my insert code there it will solve the issue. But I have lot of code that I will have to rewrite :-(. Is there any other solution that will allow me to know the actual current date? What if the internet is off?

Thanking you in advance

Sam
  • 2,972
  • 6
  • 34
  • 62
  • 1
    Does this answer your question? [Android : Get current date and time from firebase](https://stackoverflow.com/questions/49865034/android-get-current-date-and-time-from-firebase) – Aravindraj Jan 25 '20 at 15:58

2 Answers2

1

Your only option that doesn't involve writing backend code is to use security rules to restrict database writes based on the current date. What you will have to do is set up a rule on the collection that should be restricted, and it will have a line that looks something like this:

allow write: if request.time < timestamp.date(yyyy, mm, dd);

Where yyyy, mm, and dd are the components of the date. If you want something more specific, read the security rules API docs for timestamp functions - you will have to provide an time in epoch milliseconds. Note that timestamps in security rules are always measured in UTC.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
1

You could maybe write a small amount of backend code to read the Firebase server timestamp and use that to check the cutoff. See this code:

exports.currentTime = functions.https.onRequest((req, res) => {
    res.send({"timestamp":new Date().getTime()})
})

I got this from this SO answer so don't ask me for more details :-)

How to save the current date/time when I add new value to Firebase Realtime Database

Not sure about how to handle the Internet off... but if you can't read the server date, they can't submit the results.

GrahamD
  • 2,952
  • 3
  • 15
  • 26