0
votes

I want to create a custom token without using a service account json. Tried the below config items: https://firebase.google.com/docs/auth/admin/create-custom-tokens#using_a_service_account_id

https://firebase.google.com/docs/auth/admin/create-custom-tokens#letting_the_admin_sdk_discover_a_service_account

and used the below code to generate a token

admin.auth().createCustomToken(uid)
  .then(function(customToken) {
    // Send token back to client
  })
  .catch(function(error) {
    console.log('Error creating custom token:', error);
  });

Getting the below error :

'Failed to determine service account. Make sure to initialize the SDK with a service account credential. Alternatively specify a service account with iam.serviceAccounts.signBlob permission. Original error: Error: Error while making request: getaddrinfo ENOTFOUND metadata metadata:80. Error code: ENOTFOUND'

Used the latest "firebase-admin" npm module.

Kindly advice.

3
Unless your code is running in a Google managed environment (Cloud Function, etc) the only way you can use is the service account JSON file.Herohtar

3 Answers

1
votes

Short answer: You cannot, the sdk key is the only way to authenticate and authorize your app in order to grant permissions.

Long (not that long) answer: use a cloud function, if it is into your project, it will init it for you, so you don't have to use the key to instantiate the sdk

0
votes

If your code is not running in a managed runtime in Google Cloud, you must either provide a service account JSON file or at very least the serviceAccountId app option.

admin.initializeApp({
  serviceAccountId: 'my-client-id@my-project-id.iam.gserviceaccount.com',
});
0
votes

You should initialize admin by passing your service account key(Download it from Firebase) to credential first. Use this code before calling admin.auth().

var serviceAccount = require('./ServiceAccountKey.json')

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