Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: update google one-tap docs (#28723)

Kang Ming7fa23150acec
diff --git a/apps/docs/content/guides/auth/social-login/auth-google.mdx b/apps/docs/content/guides/auth/social-login/auth-google.mdx
index 2c2644b17df..3d864727e99 100644
--- a/apps/docs/content/guides/auth/social-login/auth-google.mdx
+++ b/apps/docs/content/guides/auth/social-login/auth-google.mdx
@@ -68,7 +68,7 @@ To use Google's pre-built signin buttons:
1. Go to the [API Credentials page](https://console.cloud.google.com/apis/credentials).
1. Click `Create credentials` and choose `OAuth Client ID`.
1. For application type, choose `Web application`.
-1. Under **Authorized JavaScript origins** and **Authorized redirect URLs**, add your site URL. This is the URL of the website where the signin button will appear, _not_ your Supabase project domain.
+1. Under **Authorized JavaScript origins** and **Authorized redirect URLs**, add your site URL. This is the URL of the website where the signin button will appear, _not_ your Supabase project domain. If you're testing in localhost, ensure that you have `http://localhost` set in the **Authorized JavaScript origins** section as well. This is important when integrating with Google One-Tap to ensure you can use it locally.
1. When you finish configuring your credentials, you will be shown your client ID. Add this to the **Authorized Client IDs** field in the [Google Auth Provider section of the Supabase Dashboard](/dashboard/project/_/auth/providers). Leave the OAuth client ID and secret blank. You don't need them when using Google's pre-built approach.
</TabPanel>
@@ -341,6 +341,96 @@ Most web apps and websites can utilize Google's [personalized sign-in buttons](h
// Use 'nonce' when invoking the supabase.auth.signInWithIdToken() method
```
+### One-tap with NextJS
+
+If you're integrating Google One-Tap with your NextJS application, you can refer to the example below to get started:
+
+```tsx
+'use client'
+
+import Script from 'next/script'
+import { createClient } from '@/utils/supabase/client'
+import { CredentialResponse } from 'google-one-tap'
+import { useRouter } from 'next/navigation'
+import { useEffect } from 'react'
+
+const OneTapComponent = () => {
+ const supabase = createClient()
+ const router = useRouter()
+
+ // generate nonce to use for google id token sign-in
+ const generateNonce = async (): Promise<string[]> => {
+ const nonce = btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32))))
+ const encoder = new TextEncoder()
+ const encodedNonce = encoder.encode(nonce)
+ const hashBuffer = await crypto.subtle.digest('SHA-256', encodedNonce)
+ const hashArray = Array.from(new Uint8Array(hashBuffer))
+ const hashedNonce = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('')
+
+ return [nonce, hashedNonce]
+ }
+
+ useEffect(() => {
+ const initializeGoogleOneTap = () => {
+ console.log('Initializing Google One Tap')
+ window.addEventListener('load', async () => {
+ const [nonce, hashedNonce] = await generateNonce()
+ console.log('Nonce: ', nonce, hashedNonce)
+
+ // check if there's already an existing session before initializing the one-tap UI
+ const { data, error } = await supabase.auth.getSession()
+ if (error) {
+ console.error('Error getting session', error)
+ }
+ if (data.session) {
+ router.push('/')
+ return
+ }
+
+ /* global google */
+ google.accounts.id.initialize({
+ client_id: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
+ callback: async (response: CredentialResponse) => {
+ try {
+ // send id token returned in response.credential to supabase
+ const { data, error } = await supabase.auth.signInWithIdToken({
+ provider: 'google',
+ token: response.credential,
+ nonce,
+ })
+
+ if (error) throw error
+ console.log('Session data: ', data)
+ console.log('Successfully logged in with Google One Tap')
+
+ // redirect to protected page
+ router.push('/')
+ } catch (error) {
+ console.error('Error logging in with Google One Tap', error)
+ }
+ },
+ nonce: hashedNonce,
+ // with chrome's removal of third-party cookiesm, we need to use FedCM instead (https://developers.google.com/identity/gsi/web/guides/fedcm-migration)
+ use_fedcm_for_prompt: true,
+ })
+ google.accounts.id.prompt() // Display the One Tap UI
+ })
+ }
+ initializeGoogleOneTap()
+ return () => window.removeEventListener('load', initializeGoogleOneTap)
+ }, [])
+
+ return (
+ <>
+ <Script src="https://accounts.google.com/gsi/client" />
+ <div id="oneTap" className="fixed top-0 right-0 z-[100]" />
+ </>
+ )
+}
+
+export default OneTapComponent
+```
+
</TabPanel>
<TabPanel id="react-native" label="Expo React Native">