I can't authenticate Firebase with correct details. Get error: Make sure to initialize the SDK with a service account credential

Viewed 19

I have followed these instructions and copied my Firebase config details, yet I keep getting told it can't be authenticated. I have tried both service account json file and ID. No idea why I get error because the Firebase details are correct.

Setup:

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/database';
import 'firebase/compat/firestore';
import { attachCustomCommands } from 'cypress-firebase';
// import * as serviceAccount from '../support/serviceAccountKey.json'

const fbConfig = {
  // Your config from Firebase Console
  apiKey: "",
  authDomain: ".firebaseapp.com",
  // credential: serviceAccount,
  databaseURL: "firebaseio.com",
  projectId: "test-dev",
  serviceAccountId: 'cypress@test-dev.iam.gserviceaccount.com',
  storageBucket: "test-dev.appspot.com",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};

firebase.initializeApp(fbConfig);

attachCustomCommands({ Cypress, cy, firebase });

Login:

before(() => {
      cy.clearFirebaseAuth()
      // cy.visit(Cypress.env("baseUrl"))
      // cy.completeLoginScreen(Cypress.env("standardUserEmail"), Cypress.env("standardUserPassword"))
      // cy.verifyLogin()
      cy.request('POST', Cypress.env("baseUrlApi") + "auth", {
         email: Cypress.env("standardUserEmail"),
         password: Cypress.env("standardUserPassword"),
       }).then((response) => {
         cy.login(response.body.uid)
         // cy.setCookie('token', response.body.token)
         // window.localStorage.setItem("token", response.body.token)
          // window.localStorage.setItem(response.body.token, response.body.uid);

       })
    })

Error message:

cy.task('createCustomToken') failed with the following error:

> Failed to determine service account. Make sure to initialize the SDK with a service account credential.
1 Answers

I got it working by posting in cypress config file instead:

export default defineConfig({
  e2e: {
    async setupNodeEvents(on, config) {
      cypressFirebasePlugin(on, config, admin, {
        credential: admin.credential.cert(serviceAccount as admin.ServiceAccount)
      });
Related