Cloud Functions - User Custom Claims Returns Undefined
I created a new user using custom token with additional claims.
I can able to access the claims in Firestore security rules but in cloud functions it returns undefined.
Code for creating user:
admin.auth()
.createCustomToken(uid, claims)
.then(token => {
console.log(token);
});
Code for getting created user:
admin.auth().getUser(uid)
.then((userRecord) => {
console.log(userRecord.customClaims);
});
userRecord.customClaims returns undefined
Answer
You should use setCustomUserClaims()
. Not createCustomToken()
.
admin.auth()
.setCustomUserClaims(uid, claims)
.then(token => {
console.log(token);
});
setCustomUserClaims()
is always override, so I recommend to get current customClaims and merge.
It depends on the situation, I recommend to use async/await.
Like this.
async function setClaims(uid, claims) {
try {
const userRecord = await admin.auth().getUser(uid);
return await admin.auth().setCustomUserClaims(uid, {
...userRecord.customClaims,
...claims
});
} catch (e) {
return Promise.reject(e);
}
}
See
- https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth.html#set-custom-user-claims
- https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth.html#create-custom-token
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
Updated
setCustomUserClaims()
Sets additional developer claims on an existing user identified by the provided uid
See
- https://firebase.google.com/docs/auth/admin/custom-claims
- https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth.html#set-custom-user-claims
createCustomToken()
Creates a new Firebase custom token (JWT) that can be sent back to a client device to use to sign in with the client SDKs' signInWithCustomToken() methods.
You can also optionally specify additional claims to be included in the custom token. For example, below, a premiumAccount field has been added to the custom token, which will be available in the auth / request.auth objects in your Security Rules:
It's only available in the auth / request.auth objects in your Security Rules. So not set user customClaims.
See
Related Questions
- → How can I query Firebase for an equalTo boolean parameter?
- → How can I access nested data in Firebase with React?
- → Firebase simple blog (confused with security rules)
- → Removing item in Firebase with React, re-render returns item undefined
- → AngularJS Unknown Provider Error (Firebase & AngularFire)
- → How do you pass top level component state down to Routes using react-router?
- → "this" is null in firebase query function in reactjs
- → Angular Module Failed to Load
- → Multiple dex files define Lcom/google/android/gms/internal/zzrx;
- → Joining Firebase tables in React
- → How can I make add firepad to my reactjs project?
- → How to use Cloud Functions for Firebase to prerender pages for SEO?
- → React.js component has null state?