You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用正确信息无法认证Firebase,提示需用服务账号凭据初始化SDK

Cypress-Firebase认证失败:createCustomToken任务报错

我已按照cypress-firebase官方文档指引操作,复制了正确的Firebase配置信息,尝试过服务账号JSON文件和服务账号ID两种方式,但始终无法完成认证。执行登录逻辑时触发cy.task('createCustomToken')失败,报错信息如下:

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

初始化代码

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 });

登录代码

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);

       })
    })

内容的提问来源于stack exchange,提问作者George

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 04:26:05