SuperAPI AI 工具集

react-native-nitro-google-signin

编程与开发

查看项目

更新于 10-15 15:44

技能介绍

用途

react-native-nitro-google-signin 用于在 React Native 和 Expo 应用中集成 Google 登录(Nitro Universal Google Sign-In)。它面向编码智能体(coding agents),提供安装指引、API 规则、平台配置和故障排查参考,帮助开发者在移动应用中添加 Google 身份验证能力。

主要能力

  • 提供 GoogleOneTapSignIn API(从 react-native-nitro-google-signin 导入)。
  • 支持 One Tap 登录、静默登录(signIn)、创建账户(createAccount)和显式登录(presentExplicitSignIn)。
  • 支持 configure({ webClientId }) 配置,需在登录前调用。
  • 支持 serverAuthCode 获取,需在 configure() 中设置 offlineAccess: true,否则始终为 null;requestScopes() 仍可返回 accessToken 用于设备端 API。
  • 提供 getTokens(登录后调用,无会话时抛出 SIGN_IN_REQUIRED)、getCurrentUser(同步返回用户与 scopes 或 null)、revokeAccess(Android 按 email/id,iOS 仅当前会话)等方法。
  • 支持 checkPlayServices 检查 Google Play 服务。
  • 提供 isNoSavedCredentialFoundResponse、isSuccessResponse 等响应判断辅助函数。

使用方式

安装智能体技能:

npx skills add react-native-nitro-google-sign-in/google-signin -g -y

安装包:

bun add react-native-nitro-google-signin react-native-nitro-modules
bundle exec pod install --project-directory="ios"

要求 React Native ≥ 0.76。不支持 Expo Go,需使用 expo-dev-client。

登录流程示例:

import {
  GoogleOneTapSignIn,
  isNoSavedCredentialFoundResponse,
  isSuccessResponse,
} from 'react-native-nitro-google-signin'

GoogleOneTapSignIn.configure({ webClientId: 'autoDetect' })

await GoogleOneTapSignIn.checkPlayServices()
let response = await GoogleOneTapSignIn.signIn()
if (isNoSavedCredentialFoundResponse(response)) {
  response = await GoogleOneTapSignIn.createAccount()
}
if (isNoSavedCredentialFoundResponse(response)) {
  response = await GoogleOneTapSignIn.presentExplicitSignIn()
}
if (isSuccessResponse(response)) {
  const { user, idToken } = response.data
}

平台配置要点:

  • Expo:使用插件 react-native-nitro-google-signin,在 app.config.js 中配置 googleServicesFile,原生配置后执行 expo prebuild。
  • Bare Android:配置 SHA-1,可选 Google Services 插件;自动检测需要 google-services.json 和 com.google.gms.google-services 插件。
  • Bare iOS:配置 GoogleService-Info.plist 和 REVERSED_CLIENT_ID URL scheme。
  • 原生变更后需重新构建应用,仅靠 Metro 不够。

适用场景

  • 在 React Native 或 Expo 应用中添加 Google 登录。
  • 需要 One Tap 登录、静默登录或显式登录流程。
  • 需要获取 idToken 并在服务端校验(签名、aud、iss、exp;使用 hostedDomain 时校验 JWT hd)。
  • 需要 serverAuthCode 进行后端离线访问授权。
  • 需要管理令牌、获取当前用户或撤销访问权限。

限制

  • 要求 React Native ≥ 0.76。
  • 不支持 Expo Go,必须使用 expo-dev-client。
  • iOS 静默 signIn() 返回 serverAuthCode: null,初始 code 需使用 createAccount()。
  • revokeAccess 在 iOS 上仅限当前会话,id 不匹配会抛出异常。
  • getTokens 在无会话时抛出 SIGN_IN_REQUIRED;iOS 在 401 后重试前需调用 clearCachedAccessToken。
  • Android 自动检测需要 google-services.json 和 Gradle 插件。
  • 原生变更后必须重新构建应用。
  • 文档中未提供收费、价格或效果相关说明。

更多细节可参考 reference.md(API、Expo/bare 设置、错误)和 examples.md(可复制片段),文档地址:https://react-native-nitro-google-sign-in.github.io/