Setting Custom claims manually in firebase console [duplicate]

戏子无情 提交于 2020-06-17 09:34:17

问题


Is there a way I can assign admin roles or custom claims to a user mannually in the firebase console without going through firebase functions. Basically I want get one user in the database with all the roles for testing purposes


回答1:


At the time of writing, there isn't any way to add/remove Custom Claims manually from the Firebase console.

You need indeed to use a Cloud Function.


I usually use a temporary Cloud Function to set claims to a specific user, as follows:

exports.assignSpecificClaim = functions.firestore
    .document('tempoSpecificClaim/{tempoId}')
    .onCreate((snap, context) => {

        const claims = {};
        claims['admin'] = true;
        claims['batman'] = true;

        const userId = '26sn9Fvn0hNNG7cs4M1agjzhN3y2';

        return admin.auth().setCustomUserClaims(userId, claims);
    });

As you can see, this Function is triggered by the creation of a document in a temporary tempoSpecificClaim Firestore Collection.

Here is how to use it:

  • First, you add a specific security rule to your Firestore Security rules: match /tempoSpecificClaim/{tempoDoc} {allow read, write: if false;}. This way it is only possible to create a doc in the tempoSpecificClaim collection from the Firebase Console.

  • You paste the user uid in the Cloud Function, you re-deploy it and create a dummy document in the Collection. The Cloud Function is triggered and the claims are set.

  • When done with your tests, you delete the tempoSpecificClaim collection together with the corresponding security rule, you delete the temporary Cloud Function in the index.js and redeploy it.

It is not a very elaborated and elegant method, but it does the job…


Note that you could also set Claims from a server that you fully control (i.e. a “privileged server environment” referred to in the doc) in which you can use one of the Admin SDKs, Node.js, Java, Python,Go or C#.



来源:https://stackoverflow.com/questions/62357125/setting-custom-claims-manually-in-firebase-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!