4
votes

In a Firebase Cloud Function running Express, I am attempting to set custom user claims when a client posts a token to a setCustomClaims route. When I call admin.auth().setCustomUserClaims(uid, {admin: true}) within that route, I get an error saying this is "not a function."

My authentication provider is the email/password provider via Firebase authentication (i.e. I am not creating custom tokens).

Do I have to be creating custom tokens to set custom user claims?

Here is my cloud function code:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
import express from "express"

admin.initializeApp(functions.config().firebase);
const app = express()

app.post('/setCustomClaims', (req, res) => {
    uid = "some-uid"
    admin.auth().setCustomUserClaims(uid, {admin:true}).then(()=> {
        res.end(JSON.stringify( { status: 'success' } ) );
    })
});

export let api = functions.https.onRequest((request, response) => {
  if (!request.path) {
    request.url = `/${request.url}` // prepend '/' to keep query params if any
  }
  return app(request, response)
})
2
does it say that setCustomUserClaims is not a function? or by any chance the res.end( typo is also in your real code? - user2520818
The exact error is: TypeError: admin.auth(...).setCustomUserClaims is not a function. - sketchedin
The setCustomUserClaims was introduced very recently in version 5.3 of the Firebase Admin SDK for Node.js. Can you check what version of firebase-admin you are on? - Frank van Puffelen
Ah! I am running 5.2.1. I'm going to update now. Thank you, Frank. - sketchedin
In "firebase-admin": "^8.12.1" it's not working, I encountered the same issue! @sketchedin - Kavin Raju S

2 Answers

0
votes
npm install firebase-admin@latest --save

firebase-admin@5.4.3 work, good luck for fun app.

Note: client needs this code

// Force token refresh. The token claims will contain the additional claims.
firebase.auth().currentUser.getIdToken(true);
-2
votes

In the new SDKs, you no longer instantiate a database references via new Firebase. Instead, you will initialize the SDK via firebase.initializeApp():

BEFORE

var ref = new Firebase("https://databaseName.firebaseio.com");

AFTER

 // See https://firebase.google.com/docs/web/setup#project_setup for how to

 // auto-generate this config
 var config = {
 apiKey: "apiKey",
 authDomain: "projectId.firebaseapp.com",
 databaseURL: "https://databaseName.firebaseio.com"
 };

 firebase.initializeApp(config);

 var rootRef = firebase.database().ref();>

I have found same issue on the stackoverflow, check this: firebase.database is not a function