Web 端 Supabase Google Login
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
本指南将帮助你在 Web 端集成 Google 登录与 Supabase 身份验证。假设你已完成以下操作:
实现
“实现”章节完整实现位于 示例应用的 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);}重要:重定向处理
标题为“重要:重定向处理”的章节重要:重定向处理
在 Web 端使用 Google 登录时,你 必须 重定向发生时,调用插件中的任意函数来初始化插件,使其能处理重定向并关闭弹窗。你可以调用 isLoggedIn() 或者 initialize() - 两者都会触发重定向处理。
这对 OAuth 弹窗流程正常工作至关重要。
实现示例
标题为“实现示例”的章节把这段代码添加到处理 Google Sign-In 的组件中:
import { useEffect } from 'react';import { SocialLogin } from '@capgo/capacitor-social-login';
function SupabasePage() { // Check Google login status on mount to invoke redirect handling // This doesn't serve any functional purpose in the UI but ensures // that any pending OAuth redirects are properly processed useEffect(() => { const checkGoogleLoginStatus = async () => { try { await SocialLogin.isLoggedIn({ provider: 'google' }); // We don't use the result, this is just to trigger redirect handling } catch (error) { // Ignore errors - this is just for redirect handling console.log('Google login status check completed (redirect handling)'); } };
checkGoogleLoginStatus(); }, []);
// ... rest of your component}请参阅 SupabasePage.tsx 以查看完整示例。
工作原理
标题为“工作原理”的章节有关认证流程(包括 nonce 生成、JWT 校验和 Supabase 登录)的详细说明,请参阅 通用设置指南中的“工作原理”部分.
如需查看完整的 code 参考文档,请参阅 常规设置指南中的完整 Code 参考部分.
另请参阅:
- SupabasePage.tsx - 带重定向处理的示例组件
- SupabaseCreateAccountPage.tsx - 创建账号页示例
重要注意事项
标题为“重要注意事项”的部分重定向处理
标题为“重定向处理”的章节在 Web 端使用 Google 登录时,你 必须 重定向发生时,调用插件中的任意函数来初始化插件,使其能处理重定向并关闭弹窗。你可以调用 isLoggedIn() 或者 initialize() - 两者都会触发重定向处理。
这是 OAuth 弹窗流程正常运行的必要条件。如果没有它,认证完成后弹窗窗口将无法关闭。
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 中的参数
自动重试
标题为“自动重试”的章节该实现包含自动重试逻辑:
- 如果首次 JWT 验证失败,则登出并重试一次
- 这可以处理缓存 token 中 nonce 可能不正确的情况
- 如果重试也失败,则返回错误
故障排除
“故障排除”章节如果身份验证失败:
- 重定向不生效:确保你正在调用
isLoggedIn()在组件挂载时(参见上方示例) - Audience 无效: 验证你的 Google Client ID 在 Google Cloud Console 和 Supabase 中是否一致
- 授权重定向 URL: 请检查 Google Cloud Console 和 Supabase 中是否都配置了授权重定向 URL
- Nonce 不匹配: 检查控制台日志 - 函数会自动重试,但如有需要,你可以先手动退出登录
- Token 验证失败:请确保使用
mode: 'online'在 initialize 调用中获取 idToken - 请查阅 示例应用 code 仅供参考
从 Web 端 Supabase Google Login 继续
标题为“继续了解 Supabase Google 登录(Web)”的章节如果你正在使用 Web 端 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 中的实现细节, 双因素认证 有关双重身份验证的实现细节。