2024-10-15 Web Development
How to Enable Google Auth for Supabase in a Next.js App
By O. Wolfson
Adding Google Sign-In to your Supabase-powered Next.js app involves configuring Supabase, setting up the Google Cloud project for OAuth credentials, and integrating the sign-in flow in your Next.js code. Here's a clear breakdown of the steps.
1. Supabase Setup
a. Enable Google Provider in Supabase
- Go to your Supabase dashboard.
- Navigate to Authentication > Providers.
- Enable Google Sign-In by toggling the Google provider.
- You'll need the Client ID and Client Secret from Google (explained in the next section) to complete the setup.
- Copy the Callback URL from the Supabase dashboard (it looks like
https://<your-supabase-url>.supabase.co/auth/v1/callback
).
b. Set Up OAuth Credentials in Supabase
- Once you obtain the Google Client ID and Client Secret, enter them in the Google provider section of the Supabase dashboard.
2. Google Cloud Setup
a. Create a Project in Google Cloud
- Go to Google Cloud Console.
- Create a new project (or use an existing one).
b. Configure OAuth Consent Screen
- In the OAuth consent screen section, configure the consent screen (app name, privacy policy, etc.).
- Add your Supabase domain (
<your-supabase-url>.supabase.co
) to Authorized Domains.
c. Create OAuth 2.0 Credentials
- Navigate to API & Services > Credentials.
- Click Create Credentials > OAuth Client ID.
- Set the Application Type to Web application.
- Under Authorized JavaScript Origins, add your Next.js app’s URLs:
- Example for production:
https://your-nextjs-app.com
- Example for local development:
http://localhost:3000
- Example for production:
- Under Authorized Redirect URIs, add your Supabase callback URL from Step 1.
- Click Create and note down the Client ID and Client Secret.
3. Next.js Setup
a. Install Supabase Client
- Install Supabase client in your Next.js project:
b. Google Sign-In Component
Create a component to handle Google Sign-In, and use Google's Sign-In script to generate the login button.
c. Integrate the GoogleSignIn Component in Your Login Page
d. Environment Variables
Ensure you have the correct environment variables in your Next.js app:
Add the NEXT_PUBLIC_GOOGLE_CLIENT_ID
in your .env.local
file for both local and production environments.
Conclusion
- In Supabase: Enable the Google provider, set up OAuth credentials.
- In Google Cloud: Create a project, configure OAuth credentials, and set up authorized URLs.
- In Next.js: Use the Supabase client and Google Sign-In script to handle user authentication.
With these steps, you can easily integrate Google authentication in your Next.js app with Supabase.