跳过内容

Supabase Google Login on iOS

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

Section titled “Implementation”

完整的实现可在

example app’s 文件中找到。该指南将解释关键概念和如何使用它。 supabaseAuthUtils.ts 使用身份验证助手

This guide will help you integrate Google Sign-In with Supabase Authentication on iOS. It is assumed that you have already completed:__CAPGO_KEEP_0__

使用身份验证助手

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);
}

如何工作 重要注意事项.

iOS令牌缓存和 nonce 问题

iOS令牌缓存和 nonce 问题

Section titled “Using the Authentication Helper”

Section titled “How It Works”

iOS Nonce Caching Issue

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

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

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

解决方案: Google 将自动处理此问题,通过注销并重试,迫使 Google 生成一个带有正确 nonce 的新令牌。

Manual Workaround (如果自动重试不起作用):

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

确保获得带有正确 nonce 的新令牌。

有关完整的code参考,请参见《通用设置指南》中的 完整Code参考.

nonce 实现遵循来自 React Native Google Sign In 文档:

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

自动重试机制

自动重试机制

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

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

它自动处理iOS令牌缓存问题。

如果身份验证失败:

  • 令牌不匹配: 函数自动重试 - 检查控制台日志以获取详细信息。如果持续存在,请手动注销
  • 无效受众: 确保在Google Cloud Console和Supabase中(包括iOS和Web客户端ID)匹配您的Google Client ID
  • Token 验证失败:确保您正在使用 mode: 'online' 在初始化调用中获取 idToken
  • Info.plist 配置:确保 Info.plist 有正确的 URL schemes 和 GIDClientID
  • 查看 示例应用 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 的实现细节,以及 双因素身份验证 对于双因素身份验证的实现细节