I am trying to read the user's custom claims and I would like to return the values of the claims to the client individually.
This code has worked for months. However, all of a sudden it returns the error "Object is possibly undefined".
This code appears on the following lines:
role: userRecord.customClaims["role"],
type: userRecord.customClaims["type"],
Here is the entire code snippet:
exports.checkAuthClaim = functions.https.onCall(async (data, context) => {
const userId = data.userId;
return admin
.auth()
.getUser(userId)
.then((userRecord) => {
console.log(userRecord.customClaims);
return {
role: userRecord.customClaims["role"],
type: userRecord.customClaims["type"],
};
});
});
Any help is appreciated :)
the user maybe undefined, this could happen if you use invalid userId. Use optional chaining to get rid of this error.
return {
role: userRecord?.customClaims?.["role"],
type: userRecord?.customClaims?.["type"],
};
simply check for the undefined