In MeteorJs how do I refer to an existing mongo collection? - meteor

I'm using alanning:roles module, this creates 'roles' collection.
How do I reference that collection on my application?
Basically what I'm trying to do is fetch a single role based on role._id.
ie. roles.findOne('idhere');
Looking at the alanning:roles docs, they didn't include an api to get by id.
Thanks!

You can access Roles collection via
Meteor.roles.findOne({_id:'yourID'});
Snapshot of how can you check if it works via console

Related

Firebase Cloud Firestore: Creating a new document in a collection for every UID

I want to implement a badge system into my Firebase project, but I already have a lot of users. I want to take all the UIDs of my existing users, and create a new corresponding document for them in my new collection, userinfo.
I've searched the docs and I cannot find a way to do this. Can I get some help creating the documents?
If you want to create a new document for each user that already exists in the (users) collection to another (userinfo) collection, then you have to read all documents from the "users" collection and perform a write operation in the "userinfo" collection. But bear in mind to not perform such an operation on the client, but in a trusted environment, such as a server that you control, or using Cloud Functions for Firebase.

Securely setting the first custom claim on a Firebase user

What is the standard, secure way to set the first custom claim on all Firebase users?
Firebase provides some great documentation and examples for understanding and using custom claims -- e.g. this great video example -- but most examples use an existing custom claim to authorize the creation of other custom claims; and as of this post the Firebase console provides no way to set/edit/view custom claims, nor can custom claims be set via the CLI.
Here are some options I am considering:
Create a distinct admin project, which can be used by a service account to create custom claims via the Firebase Admin SDK.
Use a Cloud Function to perform custom claim creation iff a certain Firebase console action is taken, e.g. creating a Firestore Document in collection inaccessible via security rules.
Ignore security for the creation of the first custom claim; only add security after this is already a custom claim on a Firebase user.
Have you encountered this problem and solved it more-elegantly?
There is no real standard way to set Custom Claims. The only constraint, as you know, is that they can only be set from a privileged server environment by the Firebase Admin SDK, i.e. from one of your servers, or, easier and more serverless-oriented, via a Cloud Function.
So, within this constraint, you can do whatever you want. The first two options in your question are totally valid and good ones, IMO. I've wrote an article about a year ago (How to create an Admin module for managing Firebase users access and roles) in which we use a Callable Cloud Function to do the job. Today, in most of my projects, I prefer to use a Firestore collection which triggers the Cloud Function, but it is more or less equivalent (the Callable Cloud function in the article actually creates a Firestore doc).
In this article, I share a simple approach for creating the first Claim (which I call the Admin user Claim): use a temporary Cloud Function that you trigger by creating a doc in a temporary, secured, Firestore collection. Not a very elaborated and elegant method, but it does the job...
About your third option ("Ignore security for the creation of the first custom claim") I don't think you need and should do that.
You can do as described in the article and above. In a nutshell:
Set up your system with access rights restricted to the user with the Admin Custom claim (e.g. a security rule to create a doc in the dedicated Firestore collection, or a check in a Callable Cloud Function that the caller has the Admin Claim)
Create the Admin user in the Auth service
Assign him the Admin user Claim via the method detailed above.
You are done and no security hole.
Finally, it's worth noting that a new experimental Extension dedicated to setting claims with Firestore was launched in January this year. See here and here.

use firebase.auth() to get a specific user's profile or use custom collections to store and retrieve profile info

I was expecting to see some sort of functionality to get "any" user's profile information (other than "currentUser"). Something like
firebase.auth().getUser(displayName)
or
firebase.auth().getUser(uid)
I have found the documentation below and seems like there is no way to achieve this using firebase.auth().
https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html
So I conclude that the only way to achieve this type of functionality is to store your own user/profile information in a collection and query that collection. Is this correct or am I missing something?
Any help from experienced firebase/firestore developers is appreciated.
P.s. I am using react-native.
This is something that is possible with the Admin SDK but not with the client SDKs and that's most probably on purpose, in order to limit the possibilities to get users info from the front-end, with the client SDKs.
For example, with the Node.js Admin SDK you can call the getUser() or the getUserByEmail() methods.
If you need to get users info in your front-end, you could write a Callable Cloud Function, to which you pass the uid or the email of the desired user and which calls one of these Admin SDK methods and return to the front-end one or more properties of the UserRecord returned by the method.

How to save Document Reference from Cloud Function in Cloud Firestore?

Firestore allows me to add a field with the type "Reference" to a document. It's a reference "link" to other documents inside the Store.
While I imagine this is very handy, I'm missing documentation on it.
I'm having some trouble saving a reference from a Firebase Cloud Function for example.
On the client side, I just send a regular "ref" object via the client SDK and it saved the document-reference. In nodeJS (cloud function), I get an error that the object is too deep.
What's the proper way to save a field with the type reference from the admin SDK (and/or JS client SDK)?
The Firebase Admin Node SDK was updated today and fixes this problem. https://github.com/firebase/firebase-admin-node/releases/tag/v5.4.2

Mapping Firebase Auth users to Firestore Documents

I'm trying to use Firebase's Firestore database to handle Users in my Android app.
Initially I want to use the id returned from the Auth package (a string), and set that as the id for the users Collection in the database. When creating a Document inside of the Collection users, I set a uid field to that auth package string.
I realize there is no way of setting that string to an indexable value so that queries like:
// get Auth package uid string
db.collection("users").document(auth_uid_string);
would work.
So the alternative, I guess, is to use Query.whereEqualTo("uid", auth_uid_string) which would give me the user in a list, because firestore doesn't assume the query is for a unique value.
I want to avoid the above solution, and devise a way to store the users of the app in firestore, and use the auth package to verify that I fetch the correct user. Is such a solution possible? Or should I try a different firebase service, or even just run a postgres server in heroku or something?
I am not sure how to do this in android, but with JS I am doing the following:
firebase.firestore().collection('users').doc(currentUser.uid).set(currentUser)
currentUser is the user object that the authentication function returns (I use signInWithCredential)
It is working perfectly. I am sure you can implement a similar approach in android.

Resources