指南
如何在 Capacitor 中使用 Firebase Authentication
使用 @capgo/capacitor-firebase-authentication
用于 Firebase Authentication 的 Capacitor 插件。
安装
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 |
返回反序列化后的 JSON Web Token (JWT),用于在 Firebase 服务中标识用户。 |
getRedirectResult |
返回 SignInResult (来自基于重定向的登录流程)。 |
getTenantId |
获取租户 ID。 |
isSignInWithEmailLink |
检查传入链接是否为适用于……的电子邮件链接登录 signInWithEmailLink. |
linkWithApple |
将用户账号与 Apple 身份验证提供商绑定。 |
linkWithEmailAndPassword |
将用户账号与 Email 身份验证提供商绑定。 |
linkWithEmailLink |
将用户账号与 Email 身份验证提供商绑定。 |
linkWithFacebook |
将用户账号与 Facebook 身份验证提供商绑定。 |
linkWithGameCenter |
将用户账号与 Game Center 身份验证提供商关联。 |
linkWithGithub |
将用户账号与 GitHub 身份验证提供商关联。 |
linkWithGoogle |
将用户账号与 Google 身份验证提供商关联。 |
linkWithMicrosoft |
将用户账号与 Microsoft 身份验证提供商关联。 |
linkWithOpenIdConnect |
将用户账号与 OpenID Connect 提供商关联。 |
linkWithPhoneNumber |
将用户账号与手机号身份验证提供商关联。 |
linkWithPlayGames |
将用户账号与 Play Games 身份验证提供商关联。 |
linkWithTwitter |
将用户账号与 Twitter 身份验证提供商关联。 |
linkWithYahoo |
将用户账号与 Yahoo 身份验证提供商关联。 |
reload |
如果已登录,则重新加载用户账号数据。 |
revokeAccessToken |
撤销指定的 access token。目前仅支持 Apple OAuth access token。 |
sendEmailVerification |
向当前已登录用户发送验证邮件。 |
sendPasswordResetEmail |
发送密码重置邮件。 |
sendSignInLinkToEmail |
向指定邮箱的用户发送登录邮件链接。 |
setLanguageCode |
设置身份验证操作面向用户的语言 code。 |
setPersistence |
设置当前已保存的身份验证会话的持久化类型。 |
setTenantId |
设置租户 ID。 |
signInAnonymously |
以匿名用户身份登录。 |
signInWithApple |
启动 Apple 登录流程。 |
signInWithCustomToken |
启动 Custom Token 登录流程。 |
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 |
检查应用追踪透明度(App Tracking Transparency)的当前状态。 |
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 个核心方法。Listener 和 version 辅助函数,以及每个方法的完整接口约定,都记录在 GitHub 仓库.
监听事件
addListener 会返回一个句柄。调用 handle.remove() 在屏幕卸载时,或者调用 FirebaseAuthentication.removeAllListeners() 以清除所有监听器。
完整参考文档
继续阅读:使用 @capgo/capacitor-firebase-authentication
如果你正在使用 使用 @capgo/capacitor-firebase-authentication 用来规划身份验证和账户流程,并将它与 @capgo/capacitor-social-login 在 @capgo/capacitor-social-login 中查看实现细节, @capgo/capacitor-passkey 有关 @capgo/capacitor-passkey 的实现细节, @capgo/capacitor-native-biometric 有关 @capgo/capacitor-native-biometric 的实现细节, 双因素认证 有关双因素认证的实现细节,以及 SSO (企业版) 有关 SSO (Enterprise) 的实现细节。