跳过内容

Supabase Google Login on iOS

GitHub

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

texts":["Implementation",""Implementation""","完整的实现可在","示例应用的","文件中找到。 本指南解释了关键概念和如何使用它。", 使用身份验证助手 supabaseAuthUtils.ts "使用身份验证助手"

复制到剪贴板 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 nonce 缓存问题

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

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

Why this happens: iOS Google Sign-In SDK 为性能考虑缓存令牌。当缓存的令牌返回时,它可能是使用不同的 nonce (或无 nonce) 生成的,从而导致不匹配。

The solution: 实现自动处理此问题,通过登出并重试,迫使 Google 生成一个带有正确 nonce 的新令牌。

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

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

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

For the complete code reference, see the 完成Code参考部分在通用设置指南中.

随机数实现遵循 来自React Native Google Sign In文档的模式:

  • rawNonce 转到Supabase的 signInWithIdToken()
  • Supabase生成一个 rawNonce 并与 nonceDigest 从Google Sign-In中包含的ID令牌中
  • nonceDigest (SHA-256散列,16进制编码)转到ID令牌中 nonce parameter in Google Sign-In APIs

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

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

这可以自动解决iOS令牌缓存问题。

如果身份验证失败:

  • Nonce 不匹配: 自动重试函数 - 检查控制台日志以获取详细信息。如果持续存在,请先手动注销
  • 无效的受众: 确保在 Google Cloud Console 和 Supabase 中的 Google Client IDs 匹配(包括 iOS 和 Web 客户端 ID)
  • 令牌验证失败: 确保使用 mode: 'online' 在初始化调用中获取 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 for the implementation detail in @capgo/capacitor-passkey, @capgo/capacitor-native-biometric for the implementation detail in @capgo/capacitor-native-biometric, and Two-factor authentication for the implementation detail in Two-factor authentication.