4

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)
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
sketchedin
  • 252
  • 1
  • 3
  • 10

2 Answers2

0
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);
SilverNak
  • 3,283
  • 4
  • 28
  • 44
Diep Gepa
  • 515
  • 5
  • 5
-2

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

Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30
  • I am using _admin.initializeApp(functions.config().firebase);_ to initialize the firebase reference. Does this still work? – sketchedin Oct 05 '17 at 14:37