how to add custom roles in firebase auth in flutter app?

512

The ability to set custom claims for a user is only available in our SDKs for trusted environments, such as the Admin SDK for Node.js that used in the code in your question.

This functionality is not available in the client-side SDK by design, as it'd be a huge security risk to allow this. While you may think this is acceptable, the client-side SDKs will not help you going down this path.

The only way to do this from a Flutter app, is to create a Cloud Function like the one you have, and call that from your Flutter code. This also then gives you a chance to secure the operation in Cloud Functions by checking if the user is authorized to add the claim to their account.

Share:
512
Kumar vishnu
Author by

Kumar vishnu

Updated on January 03, 2023

Comments

  • Kumar vishnu
    Kumar vishnu over 1 year

    I am trying to add Custom roles to my user in my Flutter app. I saw some tutorials which uses Cloud functions to assign roles to user using Node js language(not dart) script because one should not assign customRoles from the frontEnd side of app.

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp();
    
    exports.addAdminRole = function.https.onCall((data,context) => {
        return admin.auth.getUserByEmail(data.email).then(user => {
             return admin.auth().setCustomUserClaims(user.uid,{
                 admin: true
             })
           })
    })
    

    But as I am okay with the risks and I can't use this js code so I want to know that is there any way to setCustomRoles in dart(flutter) language ?

  • Kumar vishnu
    Kumar vishnu over 2 years
    Thanks for this info :)