Supabase Googleログイン - 一般設定
インストール手順とフル マークダウン ガイドを含むセットアップの質問をコピーしてください。このプラグインについて。
Google Sign-InとSupabase Authenticationを統合するためのCapacitor Social Login プラグインを使用して、このガイドはあなたを導きます。 この設定では、モバイルプラットフォームでネイティブのGoogle Sign-Inを使用しながら、バックエンドの認証にSupabase Authを活用できます。
開始する前に、以下のことを確認してください。
-
「 Google Login General Setup 」のガイドに従ってGoogle OAuthのクレデンシャルを設定
-
ターゲットプラットフォームのGoogle OAuthのクレデンシャルを設定するためのプラットフォーム固有のガイドを参照してください:
Enabling Google OAuth provider in Supabase
Section titled “Enabling Google OAuth provider in Supabase”-
Go to your Supabase Dashboard
-
Click on your project
-
Do go to the
Authenticationmenu
-
クリックして
Providersタブ
-
使用するプラットフォームを検索
Googleプロバイダ
-
プロバイダを有効化
-
使用するプラットフォームごとにクライアントIDを追加してください
-
Click on the
Savebutton
Voilà, you have now enabled Google Sign-In with Supabase Authentication
How Google Sign-In with Supabase Authentication Helper Works
Section titled “How Google Sign-In with Supabase Authentication Helper Works”This section explains how the Google Sign-In integration with Supabase works under the hood. Understanding this flow will help you implement and troubleshoot the authentication process.
1. Nonce Generation
Section titled “1. Nonce Generation”The implementation generates a secure nonce pair following the Supabase nonce requirements:
// Generate URL-safe random noncefunction getUrlSafeNonce(): string { const array = new Uint8Array(32); crypto.getRandomValues(array); return Array.from(array, (byte) => byte.toString(16).padStart(2, '0')).join('');}
// Hash the nonce with SHA-256async function sha256Hash(message: string): Promise<string> { const encoder = new TextEncoder(); const data = encoder.encode(message); const hashBuffer = await crypto.subtle.digest('SHA-256', data); const hashArray = Array.from(new Uint8Array(hashBuffer)); return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');}
// Generate nonce pairasync function getNonce(): Promise<{ rawNonce: string; nonceDigest: string }> { const rawNonce = getUrlSafeNonce(); const nonceDigest = await sha256Hash(rawNonce); return { rawNonce, nonceDigest };}フロー:
rawNonce: URL-safe random string (64 hex characters)nonceDigest: SHA-256 hash ofrawNonce(hex-encoded)nonceDigestGoogle Sign-In により、nonce digest が ID トークンに含まれますrawNonceSupabase に渡されるのは raw nonce です → Supabase は nonce をハッシュし、トークンの nonce と比較します
2. Google Sign-In
セクション「2. Google Sign-In」プラグインを初期化し、Google でサインインします:
await SocialLogin.initialize({ google: { webClientId: 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com', // iOS only: iOSClientId: 'YOUR_IOS_CLIENT_ID.apps.googleusercontent.com', mode: 'online', // Required to get idToken },});
const response = await SocialLogin.login({ provider: 'google', options: { scopes: ['email', 'profile'], nonce: nonceDigest, // Pass the SHA-256 hashed nonce },});3. JWT の検証
セクション: “3. JWT の検証”Supabase へのトークン送信前に、実装では JWT トークンを検証します:
function validateJWTToken(idToken: string, expectedNonceDigest: string): { valid: boolean; error?: string } { const decodedToken = decodeJWT(idToken);
// Check audience matches your Google Client IDs const audience = decodedToken.aud; if (!VALID_GOOGLE_CLIENT_IDS.includes(audience)) { return { valid: false, error: 'Invalid audience' }; }
// Check nonce matches const tokenNonce = decodedToken.nonce; if (tokenNonce && tokenNonce !== expectedNonceDigest) { return { valid: false, error: 'Nonce mismatch' }; }
return { valid: true };}なぜ Supabase への検証を実行する必要があるのか
JWT トークンを Supabase への送信前に検証することは、以下の重要な目的を果たします:
-
無効なリクエストの防止:トークンが不正な受信者または非一致の nonce を持っている場合、Supabase はどちらの場合もトークンを拒否します。検証を実行することで、不必要な API 呼び出しを回避し、明確なエラーメッセージを提供します。
-
トークンキャッシュの問題:特に iOS の場合、Google Sign-In SDK はパフォーマンスのためにトークンをキャッシュすることがあります。キャッシュされたトークンが返される場合、キャッシュされたトークンは、異なる nonce (または nonce がない場合) で生成された可能性があり、Supabase によってトークンが “nonce の一致” エラーで拒否される可能性があります。トークンを Supabase への送信前に検証することで、早期にこの問題を検出し、自動的に新しいトークンで再試行できます。
-
セキュリティ (iOS): iOSでは、Google Client IDごとにトークンが発行されたことを検証し、他のアプリケーション用に意図されたトークンを使用する可能性のあるセキュリティ上の問題を防止します。
-
Better Error Handling: iOSのキャッシュ問題を透明に処理するために自動リトライロジックが不可欠なため、問題を検出することで、Supabaseがリトライを自動的に実行できるようになります。
検証が失敗した場合、関数は自動的に:
- Googleからログアウト (キャッシュされたトークンをクリア - iOSでは重要)
- 再度認証を試みる (正しいnonceを使用して新しいトークンを生成)
- 再試行も失敗した場合、エラーを返す
4. Supabase Sign-In
セクション「4. Supabase Sign-In」検証されたトークンは、最終的にSupabaseに送信されます:
const { data, error } = await supabase.auth.signInWithIdToken({ provider: 'google', token: googleResponse.idToken, nonce: rawNonce, // Pass the raw (unhashed) nonce});完全なCodeリファレンス
「Code」完全リファレンス」のセクション完全な実装は、 のファイルにあります。 supabaseAuthUtils.ts 、以下を含みます。
getUrlSafeNonce()- URL安全のランダムなnonceを生成するsha256Hash()- SHA-256で文字列をハッシュするgetNonce()- nonce pairを生成するdecodeJWT()- JWTトークンをデコードするvalidateJWTToken()- JWTの受信者とnonceを検証するauthenticateWithGoogleSupabase()- 自動リトライ機能付きの主な認証関数
追加の例ファイル
「追加の例ファイル」のセクション- SupabasePage.tsx -
- SupabaseCreateAccountPage.tsx - Example create account page
Next Steps
Section titled “Next Steps”Please proceed to the platform-specific setup guide for your target platform:
Keep going from Supabase Google Login - General Setup
Section titled “Keep going from Supabase Google Login - General Setup”If you are using Supabase Google Login - General Setup を使用して Using @capgo/capacitor-social-login for the native capability in Using @capgo/capacitor-social-login, @capgo/capacitor-social-login for the implementation detail in @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 __CAPGO_KEEP_0__の実装詳細については、2要素認証の詳細を参照してください。