hello,
i wish to check the data/value in firebase each time the user login via Google.
if data/value don’t exist then will write the signin email, username and membership into firebase database.
else, if data exist then no need write and direct fetch the membership value to the user profile page.
i’m jusing below blocks but after user signin the firebase database will have repeatedly writing the data inside. i’m not sure what’s wrong with my blocks.
You could use Firebase Function and create a .onCreate function that checks a node of your database, perhaps call it “members” with the emails of member users inside.
in to checkUser do above, it will check for any existing data/value in the firebase database, which if data don’t exist, it will write the name, email and membership values into database, else only pull value membership into a Label in profile page.
In Kodular, many users mistakenly write to Firebase immediately after login, without waiting for Firebase’s value existence check (FirebaseDB.GetValue) to return. This leads to data being written every time, even if it already exists.
Correct Logic:
User logs in via Google.
Get user UID (or email).
Check if /users/<uid> exists in Firebase Realtime Database.
If exists:
Fetch membership data, show on profile page.
If not exists:
Write user info (email, username, membership) to /users/<uid>.
Then, fetch membership data and show on profile page.
As soon as a user logs in, the functions.auth.user().onCreate function creates a node in YOURPROJECTBUCKET/users/${uid}/userinfo containing all the Google/providerEmail data in this case: uid, username, email, phoneNumber, displayName, photoURL, and provider.
(I used to handle the username by retrieving everything before the “@” in the email for example, mario.rossi@gmail.com would become the username mario_rossi)
If you feel it’s too complex to manually edit, you could use an AI like ChatGPT to help you add or modify what you need.
Set global uid to get idToken
Set global email to get Email
You can also use Firebase_Auth.CurrentUser to get UID.
Check if user exists in Firebase DB
Call Firebase_DB.GetValue
tag = join "users/" and global uid
When Firebase_DB.GotValue
if value ≠ "" then
// User exists
// Now fetch membership
Call Firebase_DB.GetValue
tag = join "users/" and global uid and "/membership"
else
// User doesn't exist
// Write user info to Firebase
Create empty dictionary:
email = global email
username = split at @ from email [e.g., left part]
membership = "free"
Call Firebase_DB.StoreValue
tag = join "users/" and global uid
value = dictionary
After StoreValue – Get membership
When Firebase_DB.StoreValue
Call Firebase_DB.GetValue
tag = join "users/" and global uid and "/membership"
When GotValue (membership)
Set Label_Membership.Text = value
Optional: Show Email and Username
Set Label_Username.Text = global email (or parsed name)
hi @SyntaxCore, appreciate for your reply on the block logic.
but now im thinking, i have database structure like below; how can i put such data into a meaningful structure so that later i can lookup a specific value for a specific key/tag.
for instance, if i want to know if “crispbytes@gmail.com” existed in the list/dictionary, then it will get the associated value under the “membership” key, then i want to bring this value to put on the label component at profile page.
When GoogleSignIn1.Got ID Token do:
Set global userUID to GoogleSignIn1.UserID
Set global userEmail to GoogleSignIn1.Email
Set global userName to GoogleSignIn1.DisplayName
Call FirebaseDB.Get Value
tag = join "users/" and global userUID
valueIfTagNotThere = "NOT_FOUND"
2. Firebase Got Value (Check if User Exists)
blocks
CopyEdit
When FirebaseDB.Got Value do:
If value = "NOT_FOUND" then
// User doesn't exist, write data
Call FirebaseDB.Store Value
tag = join "users/" and global userUID
value = make a dictionary with:
"email" → global userEmail
"username" → global userName
"membership" → "Free"
// Optionally, set global userMembership to "Free"
Set global userMembership to "Free"
else
// User exists, get membership from value
Set global userMembership to value["membership"]
Important Notes
Do not call FirebaseDB.Store Value unless you’ve confirmed the value is "NOT_FOUND".
The key thing is comparing the returned value and using a sentinel value like "NOT_FOUND" to detect absence.
Ensure your Firebase rules allow .read access for the path you’re checking.