Can someone help me? All I want to do is update the JWT claims in a Firebase function. So I can set flag like IsAdmin etc and use it in the realtime DB. I have seen this article https://firebase.google.com/docs/auth/admin/custom-claims but I can't get any combination to work.
This is my firebase function call that I am making after sign in. Is this problem related to debugging locally? Can I test this locally?
import * as functions from 'firebase-functions';
const cors = require('cors');
const corsHandler = cors({origin: true});
const admin = require('firebase-admin');
admin.initializeApp();
export const resetClaims = functions.https.onRequest((req : any, res : any) => {
corsHandler(req, res, () => {
admin.auth().verifyIdToken(req.header("authorization").replace("Bearer ", '')).then((claims : any) => {
var uid = claims.uid;
admin.auth().setCustomUserClaims(uid, {test: true}).then(() => {
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
}).catch((er : any)=>{
res.end(JSON.stringify({
status: er
}));
return;
});
return;
});
});
I am calling this locally from a firebase web app. Using the following code
const resetClaims = async ()=> {
const fn = firebase.functions().httpsCallable('resetClaims');
const res = await fn();
firebase.auth().currentUser.getIdToken(true);
alert(res.data.status);
}
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
resetClaims();
setUsername(user.displayName);
} else {
setUsername('');
}
});
The http post works. But returns the following when debugging locally.
{"status":{"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND\"."}}