2

this is more of a logic question. So I’m building up an app that requires 30 days trial period mechanism. What would be the right way to do this?

I’m relying on Flutter/Firebase to do this!

This is how I’m planning to do it but not sure if that is correct way to do it

  1. Adding the field named “free_trial” (boolean) in the firebase and setting it to true initially

  2. Now I need to make that field false once the user crossed the 30 days trial period! How to do this logic?

Let's assume, this is a registration page and once the user signed up, there will be a field inserted in the firebase database which would be "free_trial" and it will be initially set to true

CollectionReference users = FirebaseFirestore.instance.collection('users');
users.add({
   "free_trial": true
});

How to make it false once the user has crossed 30 days period. What logic/algorithm would be required to achieve this? like fetching today date and then checking against registration date (something like this)

Any help would be appreciated

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
shakky
  • 434
  • 5
  • 20

1 Answers1

4

A better approach would be to store the free_trial_expiration timestamp and derive whether the user is still in their free trial from that. That way you won't have to change any value when their free trial expires, you just have to check whether they are still in their free trial in your queries.

If you do want to set a field after a certain time period, you can do that with Cloud Functions. The two most common approaches are to either:

  • Periodically check all potentially expired documents (e.g. all documents that have "free_trial": true, and update the ones that have expired, OR
  • Create a Cloud Task for each trial user (tasks are quite cheap), and set that task to trigger another Cloud Function when the trial period expires, so that you can update that specific document.

For more on these approaches, also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you, sir! I will go with the first approach that you have suggested as I won't have to make any changes to the documents :) Thanks a lot! Appreciated it – shakky Jan 22 '22 at 20:10