跳过主要内容

Firebase认证 Capacitor插件

Capacitor插件

安装

bun add @capgo/capacitor-firebase-authentication bunx cap sync
npm, pnpm 或 yarn
  • npm install @capgo/capacitor-firebase-authentication
  • pnpm add @capgo/capacitor-firebase-authentication
  • yarn add @capgo/capacitor-firebase-authentication

指南

如何在Capacitor中使用 Firebase 身份验证

使用@capgo/capacitor-firebase-authentication

Capacitor 插件用于 Firebase Authentication。

安装

bun add @capgo/capacitor-firebase-authentication
bunx cap sync

bunx cap sync 将插件的本机 code 复制到您的本机项目中。每次升级插件后都重新运行它。

导入

import { FirebaseAuthentication } from '@capgo/capacitor-firebase-authentication';

API 简介

方法 描述
applyActionCode 向用户发送的验证 code
confirmPasswordReset 完成密码重置过程。
confirmVerificationCode 完成电话号码验证过程。
createUserWithEmailAndPassword 创建一个新的用户账户,使用邮箱和密码。如果新账户创建成功,用户会自动登录。
deleteUser 删除用户并注销。
fetchSignInMethodsForEmail 获取一个邮箱地址的登录方式。
getCurrentUser 获取当前登录的用户。
getPendingAuthResult 返回结果 SignInResult 获取当前登录用户的Firebase Auth ID令牌。
getIdToken 返回一个用来识别用户并访问Firebase服务的JSON Web令牌(JWT)。
getIdTokenResult 从重定向式登录流程中获取。
getRedirectResult 返回结果 SignInResult 获取租户ID。
getTenantId 检查一个链接是否是适合用于邮箱登录的链接。
isSignInWithEmailLink 检查一个链接是否是适合用于邮箱登录的链接。 signInWithEmailLink.
linkWithApple 与 Apple 身份验证提供商绑定用户帐户。
linkWithEmailAndPassword 与电子邮件身份验证提供商绑定用户帐户。
linkWithEmailLink 与电子邮件身份验证提供商绑定用户帐户。
linkWithFacebook 与 Facebook 身份验证提供商绑定用户帐户。
linkWithGameCenter 与 Game Center 身份验证提供商绑定用户帐户。
linkWithGithub 与GitHub身份验证提供商绑定用户帐户。
linkWithGoogle 与 Google 身份验证提供商绑定用户帐户。
linkWithMicrosoft 与 Microsoft 身份验证提供商绑定用户帐户。
linkWithOpenIdConnect 与 OpenID Connect 身份验证提供商绑定用户帐户。
linkWithPhoneNumber 与手机号身份验证提供商绑定用户帐户。
linkWithPlayGames 与 Play Games 身份验证提供商绑定用户帐户。
linkWithTwitter 与 Twitter 身份验证提供商绑定用户帐户。
linkWithYahoo 与 Yahoo 身份验证提供者关联用户账户。
reload 如果已登录,重新加载用户账户数据。
revokeAccessToken 撤销指定的 Apple OAuth 访问令牌。
sendEmailVerification 向当前已登录用户发送验证邮件。
sendPasswordResetEmail 发送密码重置邮件。
sendSignInLinkToEmail 向指定邮箱的用户发送登录邮件链接。
setLanguageCode 设置用于身份验证操作的用户界面语言 code。
setPersistence 设置当前保存的身份验证会话的持久性类型。
setTenantId 设置租户 ID。
signInAnonymously 以匿名用户身份登录。
signInWithApple 启动 Apple 登录流程。
signInWithCustomToken 启动自定义令牌登录流程。
signInWithEmailAndPassword 使用电子邮件和密码启动登录流程。
signInWithEmailLink 使用电子邮件和登录电子邮件链接登录。
signInWithFacebook 启动Facebook登录流程。
signInWithGameCenter 启动Game Center登录流程。
signInWithGithub 启动GitHub登录流程。
signInWithGoogle 启动Google登录流程。
signInWithMicrosoft 启动Microsoft登录流程。
signInWithOpenIdConnect 启动OpenID Connect登录流程。
signInWithPhoneNumber 使用电话号码启动登录流程。
signInWithPlayGames 启动Play Games登录流程。
signInWithTwitter 启动Twitter登录流程。
signInWithYahoo 启动Yahoo登录流程。
signOut 开始注销流程。
unlink 从用户帐户中解除一个提供者。
updateEmail 更新当前登录用户的电子邮件地址。
updatePassword 更新当前登录用户的密码。
updateProfile 更新用户的个人资料。
useAppLanguage 设置用户可见的语言code为应用的默认语言。
useEmulator 为您的应用配置与身份验证模拟器通信的功能。
verifyBeforeUpdateEmail 在更新当前登录用户的电子邮件地址之前,验证新电子邮件地址。
checkAppTrackingTransparencyPermission 检查应用跟踪透明度的当前状态。
requestAppTrackingTransparencyPermission 打开系统对话以授权应用跟踪透明度。

示例

applyActionCode()

应用一个通过电子邮件发送给用户的验证code。

import { FirebaseAuthentication } from '@capgo/capacitor-firebase-authentication';

await FirebaseAuthentication.applyActionCode({ oobCode: 'oob-code' });

confirmPasswordReset()

完成密码重置过程。

import { FirebaseAuthentication } from '@capgo/capacitor-firebase-authentication';

await FirebaseAuthentication.confirmPasswordReset({
  oobCode: 'oob-code',
  newPassword: 'new-password',
});

confirmVerificationCode()

完成手机号验证过程。

import { FirebaseAuthentication } from '@capgo/capacitor-firebase-authentication';

const result = await FirebaseAuthentication.confirmVerificationCode({
  verificationId: 'phoneCodeSent',
  verificationCode: 'phoneCodeSent',
});
// The result holds sensitive values: use it without logging it.

createUserWithEmailAndPassword()

创建一个新用户帐户,使用电子邮件和密码。 如果新帐户创建成功,则用户会自动登录。

import { FirebaseAuthentication } from '@capgo/capacitor-firebase-authentication';

const result = await FirebaseAuthentication.createUserWithEmailAndPassword({
  email: 'user@example.com',
  password: 'password',
});
// The result holds sensitive values: use it without logging it.

deleteUser()

删除用户并注销。

import { FirebaseAuthentication } from '@capgo/capacitor-firebase-authentication';

await FirebaseAuthentication.deleteUser();

fetchSignInMethodsForEmail()

获取一个电子邮件地址的登录方式。

import { FirebaseAuthentication } from '@capgo/capacitor-firebase-authentication';

const result = await FirebaseAuthentication.fetchSignInMethodsForEmail({ email: 'user@example.com' });
console.log(result);

上表列出了 59 个核心方法。 监听器和版本帮助,以及每个方法的完整契约,均在 GitHub 仓库.

监听事件

addListener 返回一个句柄。 在屏幕卸载时调用 handle.remove() 或清除所有监听器。 FirebaseAuthentication.removeAllListeners() 全局参考

__CAPGO_KEEP_0__

继续使用 @capgo/capacitor-firebase-authentication

如果您正在使用 使用 @capgo/capacitor-firebase-authentication 来规划身份验证和帐户流程,连接它与 @capgo/capacitor-社交登录 查看 @capgo/capacitor-social-login 的实现细节 @capgo/capacitor-认证密钥 查看 @capgo/capacitor-passkey 的实现细节 @capgo/capacitor-原生生物识别 为 @capgo/capacitor-native-biometric 的实现细节 双因素认证 为双因素认证和 SSO (企业) 为 SSO (企业) 的实现细节

常见问题

Firebase Authentication 插件常见问题

如何在 Capacitor 应用中安装 Firebase Authentication 插件?

运行 "bun add @capgo/capacitor-firebase-authentication" (或 "npm install @capgo/capacitor-firebase-authentication"), 然后运行 "bunx cap sync" 以便 iOS 和 Android 项目捕获 native code。从 "@capgo/capacitor-firebase-authentication" 导入您的应用 code。

@capgo/capacitor-firebase-authentication 与 React、Vue 和 Angular 是否兼容?

是的。Firebase Authentication 是一个 Capacitor 包,因此它与任何运行在 Capacitor 内的 Web 框架兼容,包括 Ionic、React、Vue、Angular、Svelte 和普通 JavaScript。

@Capacitor 的哪个版本 @capgo/capacitor-firebase-authentication 支持?

Capgo 插件遵循 Capacitor 的主要版本:使用与 Capacitor 主要版本匹配的插件主要版本(例如插件 v8 与 Capacitor 8)。在 GitHub README 中的兼容性表格中列出了维护的版本。

@capgo/capacitor-firebase-authentication 是否免费和开源?

是的,源码 code 在 GitHub 上公开可见:https://github.com/Cap-go/capacitor-firebase/tree/main/packages/authentication,且该包免费从 npm 安装。欢迎bug报告和pull请求。

是否可以在不经过App Store审核的情况下更新使用Firebase Authentication的 code?

安装或升级插件会改变原生 code,因此需要新建一个商店包。之后,使用 Capgo 实时更新的JavaScript、HTML和CSS代码可以立即发布。

不必等待应用程序审查就可以将 Firebase 身份验证更改推送到商店

一旦插件在您的商店构建中,Capgo 实时更新将将您的 JavaScript、HTML 和 CSS 更改推送给用户,仅需几分钟。

开始使用 Capgo