跳过内容

在 Android 上使用 Supabase Google 登录

介绍

简介

本指南将帮助您在 Android 上将 Google Sign-In 与 Supabase 身份验证集成。假设您已经完成:

完整的实现可在

示例应用程序的

文件中找到。该指南将解释关键概念并说明如何使用它。 使用身份验证助手 supabaseAuthUtils.ts __CAPGO_KEEP_0__

__CAPGO_KEEP_1__

使用身份验证助手

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

在 General Setup 指南中的 了解身份验证流程的详细说明,包括 nonce 生成、 JWT 验证和 Supabase 登录,请参见.

For the complete code reference, see the Complete Code Reference section in the General Setup guide.

重要注意事项

它是如何工作的

随机数处理

随机数处理

Capgo 的 nonce 实现遵循 React Native Google Sign In 文档中的模式 然后去 Supabase:

  • rawNonce Supabase 生成一个 signInWithIdToken()
  • 并将其与 rawNonce 从 Google Sign-In 中获取的 ID token 中的 nonceDigest (SHA-256 哈希,16 进制编码)
  • nonceDigest 参数在 Google Sign-In API 中 nonce 自动重试

实现包括自动重试逻辑:

  • 如果 JWT 验证在第一次尝试时失败,它会注销并重试一次
  • 这处理了缓存令牌可能具有错误的 nonce 的情况
  • 如果重试也失败,会返回错误

如果认证失败:

  • 无效的受众: 确保在 Google Cloud Console 和 Supabase 中的 Google Client ID 匹配
  • nonce 不匹配: 检查控制台日志 - 函数将自动重试,但如果需要,可以手动注销
  • 令牌验证失败: 确保您正在使用 mode: 'online' 在初始化调用中获取 idToken
  • 查看 示例应用 code 参考