Firebase Stripe扩展SDK v9升级:客户门户跳转代码报错求助
解决Firebase Stripe扩展SDK v9客户门户跳转的权限及代码问题
核心问题排查与修复步骤
修正函数名称的拼写错误
你的代码中函数名包含多余空格:ext-firestore-stripe-subscriptions- createPortalLink,这会导致无法正确匹配部署的云函数。请改为无空格的正确名称(可在Firebase控制台的「函数」页面确认实际函数名,通常为ext-firestore-stripe-subscriptions-createPortalLink)。确保调用者为已认证用户
该扩展的createPortalLink函数默认仅允许已登录的Firebase用户调用,未登录用户会直接返回403权限拒绝。调用函数前必须检查用户认证状态:const auth = getAuth(this.afApp); if (!auth.currentUser) { console.error("请先登录再访问客户门户"); return; }修正代码逻辑并处理跳转
你当前的代码未执行跳转操作,且未捕获错误。以下是修正后的完整代码:import { getFunctions, httpsCallable } from "firebase/functions"; import { getAuth } from "firebase/auth"; constructor(private initialize: InitializeService, private afApp: FirebaseApp) { } functions = getFunctions(this.afApp, "europe-west1"); async customerPortal() { const auth = getAuth(this.afApp); if (!auth.currentUser) { console.error("用户未登录,无法访问客户门户"); return; } try { const functionRef = httpsCallable(this.functions, 'ext-firestore-stripe-subscriptions-createPortalLink'); const result = await functionRef({ returnUrl: window.location.origin, locale: "auto" // 可选参数,按需添加 }); const portalData = result.data as { url: string }; window.location.assign(portalData.url); } catch (error) { console.error("生成客户门户链接失败:", error); } }检查扩展配置与Stripe密钥权限
- 在Firebase控制台的「扩展」页面,找到
ext-firestore-stripe-subscriptions扩展,确认配置的Stripe API key为限制权限的Secret Key(而非公钥)。 - 登录Stripe控制台,检查该Secret Key是否拥有
customers:read和customer_portal:create权限。
- 在Firebase控制台的「扩展」页面,找到
关于CORS错误
禁用浏览器CORS是无效操作,CORS错误通常是后台返回403等非成功状态码导致的次生问题,解决上述权限和代码问题后,CORS错误会自动消失。
内容的提问来源于stack exchange,提问作者Thomas Papadimitriou
相关产品推荐
相关产品推荐

