Firebase - how to set custom claim from the console [duplicate] - firebase

This question already has answers here:
How to create Firebase Authentication claims?
(3 answers)
Closed 3 years ago.
In my application I use firebase authentication and Google account as credentials provider. I would like to assign roles to users. In order to do it I would like to add custom claims to the authentication token a user gets during login. And here is my problem because I don't know how to add claims to existing user from the firebase console.

There is currently no way to set custom claims for a user in the Firebase console. If you think this would be a useful addition, file a feature request for it.
In the meantime the easiest way to add a custom claim is to do so from the a terminal window/command prompt using a small Node.js script like this:
admin.auth().setCustomUserClaims(uid, {admin: true})
Also see:
How to create Firebase Authentication claims?

Related

Check Logged in User inside Firebase functions (Not inside Flutter or web) [duplicate]

This question already has answers here:
Cloud Functions for Firebase - Get current user id [duplicate]
(1 answer)
How to protect firebase Cloud Function HTTP endpoint to allow only Firebase authenticated users?
(9 answers)
Check if User is Logged in on Firebase Auth from Node.js (Cloud Functions)
(1 answer)
How to check for authenticated users on Google Cloud Function
(1 answer)
Closed 10 months ago.
I would like to know the following
How to get the Logged in User id inside firebase functions
How to check a custom claim inside a firebase function
I know, we can check the auth inside callable function, but how about https functions
A help will be really appreciated
For https functions, you'll have to explicitly pass user's ID Token in your HTTP request (e.g. in Authorization header). Then you can verify that ID token using Firebase Admin SDK which returns a DecodedIdToken object that contains user's custom claims.
Checkout the documentation on how to retrieve user's ID Token and for more information.

How to get email of any user from the UID of Firebase in Flutter? [duplicate]

This question already has answers here:
How to get Emails of users via UID in Firebase
(1 answer)
How do I return a list of users if I use the Firebase simple username & password authentication
(7 answers)
Closed 2 years ago.
I need to get the email of any user registered in Firebase Authentication from their UID. Is that possible?
It's not possible using the APIs provided by Firebase Auth web and mobile SDKs (including Flutter). You would have to either:
Store data relevant data in a database and perform the lookup there, or
Implement a backend endpoint that uses the Firebase Admin SDK to perform the lookup

How to disable/deactivate a user account in firebase programmatically? [duplicate]

This question already has an answer here:
How to use Google relyingparty to disable user Firebase?
(1 answer)
Closed 4 years ago.
Is there any way to disable/deactivate a firebase user account programmatically?
The admin has the opportunity to do it in the Authentication Tab in the firebase console but how to do it in code?
I have not tried anything in code as i did not find any methods.
The Firebase Admin SDKs have the ability to disable user accounts. For example, on Node.js, you'd do this with:
admin.auth().updateUser(uid, {
disabled: true
})
For examples in the other supported languages, see the documentation on updating a user.
Note that the Admin SDKs are designed to be used in trusted environments only, such as your development machine, a server you control, or Cloud Functions. You cannot run this code in the client-side app, as that would allow any user to disable any other user's account.

How can one restrict Firebase Admin SDK to delete data from firebase database [duplicate]

This question already has an answer here:
Is there a way to add security rules that apply to admin in Firebase?
(1 answer)
Closed 4 years ago.
I have firebase rules to restrict users from accessing certain part of my firebase databases, but how can I restrict the user initialized with Firebase Admin SDK to delete my firebase database nodes?
Thank You
When accessing Realtime Database using the Admin SDK, it has unrestricted access to your database by default. Security rules don't apply.
The only way to restrict the Admin SDK is to initialize it with UID that forces it to behave as if it authenticated as user with that UID. This is described in the documentation using databaseAuthVariableOverride. There is also another answer on SO that describes what to do for nodejs. If you scope access like this, your security rules will have to specifically call out that UID in the rules to limit its access.

Firebase: creating an account while I am already logged in

I wanna create accounts while I am already logged in.
For example :
I'm logged in as an admin on a website and I wanna create accounts for drivers that work for me and save the uid (in a firebase collection with custom data) or something unique with what I could identify that driver#admin.com is created by admin#gmail.com.
If you want to programmatically create accounts for other users, you'll have to use the Firebase Admin SDK on a backend you control. You won't be able to do this directly from within a mobile app. You'll have to figure out some way to invoke your backend code from within your app. (Explaining all your options here is out of the scope of a Stack Overflow question.)

Resources