Jim C
Jim C

Reputation: 4385

FirebaseAuthError: `developerClaims` argument must be a valid, non-null object containing the developer claims

Final Goal: create a simple NodeJs to provide a Firestore Custom Token.

Current tentative: I am trying exactly this approach Custom Token Stackoverflow question. According to this question Author he is able to get the Custom Token. Unfortunatelly I keep getting

C:\WSs\FireStoreDemos\firestore-custom-token>node server
C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\firebase-admin\lib\auth\token-generator.js:205
            throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, errorMessage);
            ^

FirebaseAuthError: `developerClaims` argument must be a valid, non-null object containing the developer claims.
    at FirebaseAuthError.FirebaseError [as constructor] (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\utils\error.js:42:28)
    at FirebaseAuthError.PrefixedFirebaseError [as constructor] (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\utils\error.js:88:28)
    at new FirebaseAuthError (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\utils\error.js:147:16)
    at FirebaseTokenGenerator.createCustomToken (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\auth\token-generator.js:205:19)
    at Auth.BaseAuth.createCustomToken (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\auth\auth.js:94:36)
    at Object.<anonymous> (C:\WSs\FireStoreDemos\firestore-custom-token\server.js:32:14)
[90m    at Module._compile (internal/modules/cjs/loader.js:1147:30)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:996:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:896:14)[39m {
  errorInfo: {
    code: [32m'auth/argument-error'[39m,
    message: [32m'`developerClaims` argument must be a valid, non-null object containing the developer claims.'[39m
  },
  codePrefix: [32m'auth'[39m
}

All NodeJs files are:

server.js

const admin = require('firebase-admin');

exports.serviceAccount = {
  "type": "service_account",
  "project_id": "angular-firebase-auth0-3c084",
  "private_key_id": "6ba2ba41e0bf3837841aa9772c7d880b7ce3be81",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv ... deleted ... 2PV9xsYvkt8+8Ce79fYKA=\n-----END PRIVATE KEY-----\n",
  "client_email": "firebase-adminsdk-lu97a@angular-firebase-auth0-3c084.iam.gserviceaccount.com",
  "client_id": "114324662014690107039",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-lu97a%40angular-firebase-auth0-3c084.iam.gserviceaccount.com"
}

 admin.initializeApp({
   credential: admin.credential.cert(exports.serviceAccount)
});

var uid = "NSBFu2YJNDgLQJCZ99dRJlP4DRo2"; //copied from https://console.firebase.google.com/project/firetestjimis/authentication/users
var claim = {
  control: true
};
admin.auth().createCustomToken(uid, true)
  .then(function (customToken) {
    console.log(customToken)
  })
  .catch(function (error) {
    console.log("Error creating custom token:", error);
  });

package.json

{
  "name": "firebase-auth0-nodeserver",
  "version": "0.1.0",
  "description": "Node.js server to provide custom auth token.",
  "repository": "",
  "main": "server.js",
  "scripts": {
    "start": "node server"
  },
  "author": "Auth0",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-jwt": "^5.3.3",
    "firebase-admin": "^8.10.0",
    "jwks-rsa": "^1.8.0"
  },
  "devDependencies": {}
}

In case it adds something, I managed to deploy a Cloud Function that returns a Custom Token with bellow typescript. Nevertheless, I need same approach in our local Servers.

import * as functions from 'firebase-functions';
import * as admin from "firebase-admin";

export const getCustomToken = functions.https.onRequest((request, response) => {
    if (admin.apps.length < 1) {   //Checks if app already initialized
        admin.initializeApp();
    }
    const uid = "NSBFu2YJNDgLQJCZ99dRJlP4DRo2"; 

    admin.auth().createCustomToken(uid)
        .then(function (customToken) {
            console.log(customToken.toString);
            response.send(customToken);
        })
        .catch(function (error) {
            console.log("Error creating custom token:", error);
        });
});

*** edited Thanks to Frank's answer I fix this issue by "... admin.auth().createCustomToken(uid).then... ".

Nevertheless, the generated Custom Token seems useless (or maybe I udnerstood something wrong).

The desire is to have a token that allowm me to post a document to Firestore. Well, with Cloud Function above I get a Token then I post it to googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken which returns an idToken that I can use to post a document to Firestore collection. I understood the NodeJs server above would generate a token with same characteristics as the one I generated from above Cloud Function but it seems I got wrong the point.

Basically, I am trying https://stackoverflow.com/a/50690299/4148175 and it seems the question Author got it working. Funny I can do it using Cloud Function but I get issue generating the Custom Token from above NodeJs

Upvotes: 0

Views: 702

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599091

If you have syntax problems like this, the docs are your friend. From the reference documentation for Auth.createCustomToken:

createCustomToken(uid: string, developerClaims?: Object): Promise<string>

...

Parameters

uid: string The uid to use as the custom token's subject.

Optional developerClaims: Object

Optional additional claims to include in the custom token's payload.

So the second argument to createCustomToken needs to be an object with the custom claims you want to add. My guess is that the boolean in the question you linked was a valid call in an older version of the SDK.

Upvotes: 1

Related Questions