Webscript that would change Firebase database values for every user automatically - firebase

So in my Firebase database I have stored users. Every user has a key "status" which default value is "0". It changes to "1" after user has done certain things in the app.
I do not really know how to approach so that every time after 24 hours value would be changed back to "0".
Do I need some kind of a web script which can be run in cron? Is there a way to handle this inside my Firebase console?

Related

Scheduling change value in FireBase

In my Firebase Real Time Database I have a variable whose value is "false".
Is it possible to create a schedule in Firebase so that at certains times and days it turns "true" and viceversa?
I know I could do this by App, but I would like to know if it is possible to do this directly from Firebase.
Thank You.
Nothing is built into the Realtime Database itself for that, but it is a very common use case for using Cloud Functions. Have a look at the documentation on running code on a schedule for more information.
Also see:
Change the field value in Firebase Firestore automatically after a certain time
How to update a value every time in the period of 24 hour in Firebase Android
How to update a value in firebase database after a specific time, like after 10 minutes?
Delete firebase data older than 2 hours
Is there a way to schedule edits to firebase database? (predates Cloud Functions, but covers many other good options)

Firebase app - keep track of the time of user's last access?

I noticed Firebase stores user creation time and user login time ny default, but not the time the user last accessed the app. Would I need to store this info separately?
I notice some of my users show as having logged in weeks ago, yet I have been using the app everyday. Keeping track of when the user last accessed the app is useful for admin purposes.
Would I need to store this info separately?
Yes, you should record whatever value you need per-user in a database of your choosing. It doesn't have to be Firestore as you tagged here.

Dart Firebase Firestore Atomic Sign up

I'm currently trying to figure out sign-up for my app. Right now, at sign up I ask users for a username alongside an email and password (for firebase_auth).
The thing is, I don't want more than one user with a username, so I need to check my database if there already exists a user with that username before signing up the user with firebase_auth and adding this new user to my database.
I'm concerned about a race condition that could arise if two users try to create an account with the same username at the same time. I'm trying to use TransactionHandler, but im not sure exactly how I can do this as I hear that a transaction might be run up to 5 times, and we shouldn't do anything that should not be run multiple times (i.e. sign up with firebase_auth?).
Any ideas as to how I can work around this?
There is no way to create a user account and create a document in the database atomically. So you'll have to instead find a way to deal with it in your application code.
Typically this comes from thinking of account creation as a sequence of steps. For example, this is quite common:
Create account in Firebase Authentication, based on the credentials the user enters.
Have the user verify their email address, so that you can reach them.
Have the user claim their unique user name.
You'll see that none of these steps depends on a step that comes after it, so you can execute them in order. And when you do that, step 3 should work fine in a transaction that may run multiple ties.
Just keep in mind: if you want something to be unique on Firestore, you need to use that value as the IDs of your documents. There is no way with client-side access (not even with transactions) to guarantee uniqueness of values across documents. For some more questions on that topic, see:
Firestore unique index or unique constraint?
Cloud Firestore: Enforcing Unique User Names
firebase rule for unique property in firestore, which uses a single document to store all user names.
Prevent duplicate entries in Firestore rules not working

I'd like to set a value on Firebase database to True just for a certain amount of time

I can't figure out how to change a value of Firebase database and then change it back after a certain amount of time(30 min), doing everything on the server side and not by the actual device date.
I'm assuming i need Firebase functions.
In case i can't do it, is there any other way keeping Firebase as main Database?
I don't really need any code but just the logic behind it.
I would question your data model. Instead of using a boolean, you may want to consider using a timestamp.
For example, if your data model is currently something along the lines of:
Permissions
- user_id
- is_allowed (boolean)
You may want to use this instead:
Permissions
- user_id
- allow_until (timestamp)
You application code can then just check if the current time is earlier than the allow_until timestamp.
There is no logic in the Firebase Realtime Database to automatically change a value after a certain amount of time. You'll typically run such code in Cloud Functions, or in the apps in your client devices.
In both cases you can keep using the Firebase Realtime Database, as you'll just be interacting with that. From Cloud Functions you'll do that through the Admin SDK.
It's a few steps:
Create a Cloud Function that queries the database to find expired items, and changes the value on them. This code uses the Admin SDK for Node.js, but is very similar to what you'd otherwise run in a web client.
Tie that Cloud Function to a cron job that runs every minute or so (depending on how accurate you want the time-out to be). For some options, see Cloud Functions for Firebase trigger on time?
I recommend you also check out these similar questions:
Delete firebase data older than 2 hours
How to delete firebase data after "n" days (doing the same from an Android client)
How to purge old content in firebase realtime database

Cloud Functions for Firebase: get actual timestamp of database

I need to get the actual timestamp of the firebase database servers instead of the placeholder firebase.database.ServerValue.TIMESTAMP.
In order to avoid an XY problem, I will provide my two use cases:
a) I have created my own reset password system using cloud functions and JWT. A user requests to reset their password. I sign an object containing their username and the current timestamp(currently using Date.now()) and send it embedded to a link to their email. When they open the link, I need to verify the JWT and also compare it to a timestamp stored under their uid in firebase database. I also update the timestamp in the database so next time they try to use the same token, it will show up as expired(even if the 1 hour expiration I have set for the token has not passed). Is using Date.now() here enough? How can I pass the current firebase time to jwt.sign().
b) I need to store a negative version of the creationTimestamp of all posts in the database for sorting purposes. -firebase.database.ServerValue.TIMESTAMP does not work. What is the correct way to do that?
Google goes through great trouble to make sure all of their servers have times that are as closely synchronized as possible. So, if you're running code in Cloud Functions, which ends up on a Google server, and you want to know the time on another Google server (like your Realtime Database), just use Date.now(). There's no need to use the special database token for timestamp when running in Cloud Functions.
That timestamp token is intended to be useful when code is running on client devices whose clocks may not be very well synchronized.

Resources