指南
如何在 Capacitor 中使用 Firebase Authentication
使用 @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 如果您的应用程序在后台启动了一个 web 登录流程,而操作系统在此期间清理了应用程序。 |
getIdToken |
获取 Firebase Auth ID Token 以获取当前已登录用户。 |
getIdTokenResult |
返回用于识别用户的 Firebase 服务的 JSON Web Token (JWT)。 |
getRedirectResult |
返回从重定向式登录流程中获取的。 SignInResult 获取租户 ID。 |
getTenantId |
检查是否为用户提供了一个适合的电子邮件链接。 |
isSignInWithEmailLink |
使用 Apple 身份验证提供者链接用户帐户。 signInWithEmailLink. |
linkWithApple |
使用电子邮件身份验证提供者链接用户帐户。 |
linkWithEmailAndPassword |
使用 Facebook 身份验证提供者链接用户帐户。 |
linkWithEmailLink |
使用 Facebook 身份验证提供者链接用户帐户。 |
linkWithFacebook |
使用 Game Center 身份验证提供者链接用户帐户。 |
linkWithGameCenter |
__CAPGO_KEEP_0__ |
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 |
为应用添加 Authentication 模拟器的跟踪功能。 |
verifyBeforeUpdateEmail |
在更新当前已登录用户的电子邮件地址之前,验证新电子邮件地址。 |
checkAppTrackingTransparencyPermission |
检查应用跟踪透明度的当前状态。 |
requestAppTrackingTransparencyPermission |
打开系统对话框以授权应用跟踪透明度。 |
示例
applyActionCode()
通过电子邮件发送给用户的验证code。
import { FirebaseAuthentication } from '@capgo/capacitor-firebase-authentication';
await FirebaseAuthentication.applyActionCode({ oobCode: 'oob-code' });
confirmPasswordReset()
应用电子邮件中发送的验证__CAPGO_KEEP_0__。
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/capacitor-firebase-authentication
如果您正在使用 使用@capgo/capacitor-firebase-authentication 规划身份验证和帐户流程,连接它 @capgo/capacitor-社交登录 查看@capgo/capacitor-social-login的实现细节 @capgo/capacitor-passkey 查看@capgo/capacitor-passkey的实现细节 @capgo/capacitor-native-生物识别 查看@capgo/capacitor-native-biometric的实现细节 双因素身份验证 查看双因素身份验证的实现细节 SSO (企业) 查看SSO (企业)的实现细节