跳过内容

Supabase Google登录iOS

GitHub

本指南将帮助您在iOS上将Google Sign-In与Supabase Authentication集成。假设您已经完成:

Implementation

Implementation部分

完整的实现可在 示例应用程序的 supabaseAuthUtils.ts 文件中找到。 本指南解释了关键概念和如何使用它。

使用身份验证助手

使用身份验证助手部分

The 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 Nonce Caching Issue

iOS 上,Google Sign-In 可以缓存令牌,这可能导致 nonce 验证失败。该 validateJWTToken 功能检测到这一点并自动处理它:

  1. 自动检测:功能检查令牌中的 nonce 是否与预期的 nonceDigest
  2. 自动重试:如果验证失败,它会自动从 Google 登出并重试一次
  3. 错误处理:如果重试也失败,则返回错误

为什么会发生这种情况:iOS Google Sign-In SDK 缓存令牌以提高性能。当返回缓存令牌时,它可能已使用不同的 nonce(或无 nonce)生成,导致不匹配。

解决方案:实现自动处理此问题的方法是登出并重试,这迫使 Google 生成一个带有正确 nonce 的新令牌。

手动解决方案 (如果自动重试不起作用):

// Logout first to clear cached tokens
await SocialLogin.logout({ provider: 'google' });
// Then authenticate
const result = await authenticateWithGoogleSupabase();

这确保了使用正确的 nonce 获得一个新的令牌。

有关完整的 code 参考,请参见 完整的 Code 参考部分在通用设置指南.

令牌处理的实现遵循了 React Native Google Sign In 文档中的模式:

  • rawNonce 前往 Supabase 的 signInWithIdToken()
  • Supabase 生成一个 rawNonce 并与 Google Sign-In 的 nonceDigest 其中包含在 Google Sign-In ID token 中的
  • nonceDigest (SHA-256 哈希,16 进制编码) 传递到 nonce 参数在 Google Sign-In API 中

自动重试机制

自动重试机制

The authenticateWithGoogleSupabase 函数包含一个 retry 参数:

  • 首次调用 (retry=false: 如果验证失败,会自动注销并重试一次
  • Retry call (retry=true): 如果验证失败,立即返回错误

This handles the iOS token caching issue automatically.

如果认证失败:

  • Nonce 不匹配: 函数会自动重试 - 检查控制台日志以获取详细信息。如果持续存在,请手动注销
  • Invalid audience: 确认您的 Google Client IDs 在 Google Cloud Console 和 Supabase 中都匹配(包括 iOS 和 Web 客户端 ID)
  • Token 验证失败: 确保您正在使用 mode: 'online' in the initialize call to get an idToken
  • Info.plist 配置: 确保 Info.plist 有正确的 URL 方案和 GIDClientID
  • Review the 示例应用 code 以此为参考

继续从 Supabase Google Login on iOS

标题:继续从 Supabase Google Login on iOS

如果您正在使用 Supabase Google Login on iOS 来规划身份验证和帐户流程,连接它 使用 @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, 和 双因素认证 为实现细节在 双因素认证