iOS 上的 Supabase Google Login
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
本指南将帮助你在 iOS 上集成 Google Sign-In 和 Supabase Authentication。前提是你已完成:
实现
“实现”章节完整实现位于 示例应用的 supabaseAuthUtils.ts 文件。本指南介绍其核心概念及使用方法。
使用 Authentication Helper
章节标题为“使用 Authentication Helper”本指南中介绍的 authenticateWithGoogleSupabase 函数负责处理整个身份验证流程:
import { authenticateWithGoogleSupabase } from './supabaseAuthUtils';
const result = await authenticateWithGoogleSupabase();if (result.success) { console.log('Signed in:', result.user); // Navigate to your authenticated area} else { console.error('Error:', result.error);}工作原理
标题为“工作原理”的章节有关认证流程(包括 nonce 生成、JWT 校验和 Supabase 登录)的详细说明,请参阅 通用设置指南中的“工作原理”部分.
重要注意事项
标题为“重要注意事项”的章节iOS Token 缓存与 Nonce 问题
标题为“iOS Token 缓存与 Nonce 问题”的章节iOS Nonce 缓存问题
在 iOS 上,Google Sign-In 可能会缓存 token,从而导致 nonce 验证失败。 validateJWTToken 该函数会检测到这种情况并自动处理:
- 自动检测:该函数会检查 token 中的 nonce 是否与预期值匹配
nonceDigest - 自动重试:如果验证失败,会自动登出 Google 并重试一次
- 错误处理:如果重试也失败,则返回错误
为什么会这样:iOS Google 登录 SDK 会缓存 token 以提升性能。返回缓存的 token 时,它可能是用不同的 nonce(或没有 nonce)生成的,从而导致不匹配。
解决方案:实现会自动处理此问题,通过登出并重试,强制 Google 生成带有正确 nonce 的新 token。
手动解决方法 (如果自动重试不起作用):
// Logout first to clear cached tokensawait SocialLogin.logout({ provider: 'google' });
// Then authenticateconst result = await authenticateWithGoogleSupabase();这确保使用正确的 nonce 获取新的 token。
如需查看完整的 code 参考文档,请参阅 常规设置指南中的完整 Code 参考部分.
重要注意事项
标题为“重要注意事项”的部分Nonce 处理
标题为“Nonce 处理”的部分Nonce 实现遵循以下模式: React Native Google Sign In 文档:
rawNonce会发送到 Supabase 的signInWithIdToken()- Supabase 会对以下内容进行哈希处理:
rawNonce并将其与以下内容进行比对:nonceDigest该内容包含在 Google Sign-In 的 ID token 中 nonceDigest(SHA-256 哈希,十六进制编码) 会传给nonceGoogle Sign-In API 中的参数
自动重试机制
标题为“自动重试机制”的章节本指南中介绍的 authenticateWithGoogleSupabase 函数包含一个 retry 参数:
- 首次调用 (
retry=false): 如果验证失败,将自动登出并重试一次 - 重试调用 (
retry=true): 如果再次验证失败,则立即返回错误
这会自动处理 iOS 的 token 缓存问题。
故障排除
“故障排除”章节如果身份验证失败:
- Nonce 不匹配:函数会自动重试 - 查看 console logs 了解详情。如果问题仍然存在,请先手动退出登录
- Audience 无效:请确认 Google Cloud Console 和 Supabase 中的 Google Client IDs 是否一致(包括 iOS 和 Web client IDs)
- Token 验证失败:请确保使用
mode: 'online'在 initialize 调用中获取 idToken - Info.plist 配置:请确保 Info.plist 配置了正确的 URL schemes 和 GIDClientID
- 请查阅 示例应用 code 仅供参考
从 iOS 上的 Supabase Google Login 继续
标题为“继续了解 Supabase Google 登录在 iOS 上的使用”的章节如果你正在使用 iOS 上的 Supabase Google Login 用来规划身份验证和账户流程,并将它与 使用 @capgo/capacitor-social-login 关于使用 @capgo/capacitor-social-login 的原生能力, @capgo/capacitor-social-login 在 @capgo/capacitor-social-login 中查看实现细节, @capgo/capacitor-passkey 有关 @capgo/capacitor-passkey 的实现细节, @capgo/capacitor-native-biometric 以及 @capgo/capacitor-native-biometric 中的实现细节, 双因素认证 有关双重身份验证的实现细节。