跳过主要内容
@capgo/capacitor-firebase-认证 认证 & 安全 开源

Firebase 认证 Capacitor 插件

Capacitor 插件用于 Firebase 认证

安装

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 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 (企业)的实现细节

常见问题

Firebase Authentication插件常见问题

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

Run "bun add @capgo/capacitor-firebase-authentication" (or "npm install @capgo/capacitor-firebase-authentication"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-firebase-authentication" in your app code.

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

是的。 Firebase Authentication 是一个 Capacitor 包,适用于任何在 Capacitor 内部运行的 Web 框架,包括 Ionic、React、Vue、Angular、Svelte 和普通的 JavaScript。

Which Capacitor version does @capgo/capacitor-firebase-authentication support?

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

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

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-firebase/tree/main/packages/authentication and the package is free to install from npm. Bug reports and pull requests are welcome.

可以在不经过 App Store 审核的情况下更新使用 Firebase 身份验证的 code?

安装或升级插件会改变本地 code, 因此需要新建一个商店构建。 之后,JavaScript、HTML 和 CSS 的更改,调用插件的,可以立即与 Capgo 实时更新一起发布。

每月下载量97.1k

在您的商店构建中安装插件后,Capgo即刻更新将您的JavaScript、HTML和CSS更改推送给用户。

开始使用Capgo