node.jsfirebasegoogle-cloud-platformfirebase-admin

Cant access Firebase Admin SDK via Node.js on my VPS, but I can on my local machine


When running this code on my VPS:

import admin from 'firebase-admin'
import serviceAcc from '../config/test.json'

export async function fbAdminSdkTest() {
    try {
        console.log('using credentials')
        console.log(admin.credential.cert(serviceAcc as any))
        console.log('!')

        admin.initializeApp({
            credential: admin.credential.cert(serviceAcc as any),
        })

        const test = await admin.auth().createUser({
            email: 'user@example.com',
            emailVerified: false,
            phoneNumber: '+11234567890',
            password: 'secretPassword',
            displayName: 'John Doe',
            photoURL: 'http://www.example.com/12345678/photo.png',
            disabled: false,
        })
        console.log(test)
    } catch (e) {
        console.log(e)
    }
}

I get an error:

FirebaseAppError: 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: .".
    at /home/sun4/sun4portal-backend/node_modules/firebase-admin/lib/app/firebase-app.js:87:19
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async fbAdminSdkTest (/home/sun4/sun4portal-backend/test/fbAdminSdkTest.ts:11:22) {
  errorInfo: {
    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: .".'
  },
  codePrefix: 'app'
}

But on my local machine it works, and I have no idea why it doesnt on my VPS..

The line console.log(admin.credential.cert(serviceAcc as any)) prints the exact same info on both systems.

Does anyone have a clue what could be going on? All I've done in the Firebase Console is followed the instructions in "Project Settings > Service Accounts"

Local machine: Windows 10 Node 20.17 firebase-admin 12.5.0

VPS: Ubuntu 22 Node 20.17 firebase-admin 12.5.0

Thankful for any help!


Solution

  • With the additional information I got from logging the error directly from within firebase-admin I found this solution, and it works, so the issue was IPv6 not working https://stackoverflow.com/a/78445990