Is there a way to generate a push ID from the Console in the new Realtime Database? - firebase

I have a listing node in my Database and wanted to add data into it from the web console. I want to have a push ID as the key of the object. Something like this :
{
"listing": {
"4acd4g1OreMyo2FkW6DlQH7ZhvY2": {
"name": "abc",
"location": "xyz"
}
}
}
where '4acd4g1OreMyo2FkW6DlQH7ZhvY2' would be the auto generated push ID.
I know it can be done using the Android/iOS/JS SDKs (using the push method). But is this possible from the console?

No, the console doesn't support it currently. I've been using firebase for a while and I've never needed it :)

while firebase console doesn't support it officially, there is a service called mockaroo that can generate firebase random id. Make sure to pick firebase option in the format dropdown

Related

Firebase FCM fcm_options.link doesn't work

I'm using multicast to send the message to multiple users like this:
let webAppMessage = {
priority:'high',
notification:{title: titleNotif , body: `${name}: ${message}`, image: 'https://i.imgur.com/L6plOZO.png'},
tokens: webTokens,
webpush: {
fcm_options: {
link: "https://dummy.com"
}
}
admin.messaging().sendMulticast(webAppMessage)
However, clicking on the notification doesn't direct to the link.
I'm using firebase 8.6.3. I checked the other posts, but they don't resolve my issue. Is this because I'm using multicast to send the message? I can't find anything on the documentation about that.
This post solved it for me:
Firebase push notifications click does not work
Just use relative links... unlike what their documentation claims

How to prevent Meteor's accounts-base from auto-publishing the email of the current user?

According to Meteor's documentation on the Accounts package:
By default, the current user’s username, emails and profile are published to the client.
Is it possible to prevent Meteor from auto-publishing these fields? I know it's just for the user that is logged in, but that user could take a walk or be online somewhere public.
This structure of the code seems to be defined in accounts_server.js (search for autopublish and email - lines 37 and 696).
The most straightforward way to do this is going to be to modify the value of Accounts._defaultPublishFields.projection and remove the emails key. An easy way to do this while keeping the other values is to use a combination of rest and spread like so:
import { Accounts } from 'meteor/accounts-base';
const { emails, ...fields } = Accounts._defaultPublishFields.projection;
Accounts._defaultPublishFields.projection = { ...fields };
Just make sure this runs on the server and you should be good to go.

How to store keywords in firebase firestore

My application use keywords extensively, everything is tagged with keywords, so whenever use wants to search data or add data I have to show keywords in auto complete box.
As of now I am storing keywords in another collection as below
export interface IKeyword {
Id:string;
Name:string;
CreatedBy:IUserMin;
CreatedOn:firestore.Timestamp;
}
export interface IUserMin {
UserId:string;
DisplayName:string;
}
export interface IKeywordMin {
Id:string;
Name:string;
}
My main document holds array of Keywords
export interface MainDocument{
Field1:string;
Field2:string;
........
other fields
........
Keywords:IKeywordMin[];
}
But problem is auto complete reads data frequently and my document reads quota increases very fast.
Is there a way to implement this without increasing reads for keyword ? Because keyword is not the real data we need to get.
Below is my query to get main documents
query = query.where("Keywords", "array-contains-any", keywords)
I use below query to get keywords in auto complete text box
query = query.orderBy("Name").startAt(searchTerm).endAt(searchTerm+ '\uf8ff').limit(20)
this query run many times when user types auto complete search which is causing more document reads
Does this answer your question
https://fireship.io/lessons/typeahead-autocomplete-with-firestore/
Though the receommended solution is to use 3rd party tool
https://firebase.google.com/docs/firestore/solutions/search
To reduce documents read:
A solution that come to my mind however I'm not sure if it's suitable for your use case is using Firestore caching feature. By default, firestore client will always try to reach the server to get the new changes on your documents and if it cannot reach the server, it will reach to the cached data on the client device. you can take advantage of this feature by using the cache first and reach the server only when you want. For web application, this feature is disabled by default and you can enable it like in
https://firebase.google.com/docs/firestore/manage-data/enable-offline
to help you understand this feature more check this article:
https://firebase.google.com/docs/firestore/manage-data/enable-offline
I found a solution, thought I would share here
Create a new collection named typeaheads in below format
export interface ITypeAHead {
Prefix:string;
CollectionName:string;
FieldName:string;
MatchingValues:ILookupItem[]
}
export interface ILookupItem {
Key:string;
Value:string;
}
depending on the minimum letters add either 2 or 3 letters to Prefix, and search based on the prefix, collection and field. so most probably you will end up with 2 or 3 document reads for on search.
Hope this helps someone else.

Request.auth.metadata in security rules?

I have a Firebase project where I'd like for users to be able to see when other users created their profiles. My initial hope was that I could use "user.metadata.creationTime" on the frontend to pass the date into the user's extra info document and verify that it is correct by having "request.resource.data.datecreated == request.auth.metadata.creationTime" as a Database Rule, but it looks like it is not possible according to the documentation.
Is there any way I can verify that the creation date is correct on the backend?
More info edit: Below is the code that is being triggered when a user creates a new account on my profile. The three values are displayed publicly. I'm creating a niche gear for sale page so being able to see when a user first created their account could be helpful when deciding if a seller is sketchy. I don't want someone to be able to make it seem like they have been around for longer than they have been.
db.collection('users').doc(user.uid).set({
username: "Username-156135",
bio: "Add a bio",
created: user.metadata.creationTime
});
Firestore rules:
match /users/{id} {
allow get;
allow create, update: if request.resource.data.username is string &&
request.resource.data.bio is string &&
request.resource.data.created == request.auth.metadata.creationTime;
}
user.metadata.creationTime, according to the API documentation is a string with no documented format. I suggest not using it. In fact, what you're trying to do seems impossible since that value isn't available in the API documentation for request.auth.
What I suggest you do instead is use a Firebase Auth onCreate trigger with Cloud Functions to automatically create that document with the current time as a proper timestamp. Then, in security rules, I wouldn't even give the user the ability to change that field, so you can be sure it was only ever set accurately by the trigger. You might be interested in this solution overall.

How to create a push id for firebase using golang?

i want to push a map to firebase with an id (eg:-KfKoScgRhylaLjQlK-y)
fitToWorkMap := make(map[string]models.TaskFitToWork)
fitToWorkForTask :=models.TaskFitToWork{}
for i := 0; i < len(FitToWorkSlice); i++ {
fitToWorkForTask.Info =FitToWorkSlice[i]
fitToWorkForTask.DateOfCreation =time.Now().Unix()
fitToWorkForTask.Status = helpers.StatusPending
fitToWorkMap["fgsgdsfn+'i'"] = fitToWorkForTask
}
task.FitToWork = fitToWorkMap
here fitToWorkMap is a map .i want to generate a key for this map
Those keys starting with -K are called push IDs and they are automatically generated by Firebase:
When you call push() or childByAutoId in one of the supported SDKs
When you perform a POST request to the REST API
There is no Firebase SDK for Go and uses the REST API to interact with the Firebase Database. That means that it only generates a push ID when it POSTs a new node to the database. I don't know of any library for Go to generate Firebase push IDs client-side.
But luckily the keys are quite well documented in a blog post. The code for a JavaScript implementation of generatePushID() is also available, so you could port that over to Go.

Resources