Skip to main content
Google OAuthSupabase AuthNext.js 16PKCE FlowJWT

Google OAuth + Next.js + Supabase: Complete Setup Guide

Step-by-step Google OAuth setup for Next.js applications using Supabase Auth. PKCE flow, callback handling, session management, and production configuration used by OpenMyPro.

Pablo Diaz·Founder & CEO, Blossend Inc·Ex-Amazon AWS Engineer

See this stack in production. bootstrapped revenue.

Free forever. Upgrade only when you're ready.

Ex-Amazon Engineer · Healthcare Innovation

No card charged today · Independent engineering · $0 to start

Google OAuth drives 72% of OpenMyPro's sign-ups because it eliminates the biggest friction point in healthcare: creating yet another username and password. Here is the complete setup for Google OAuth with Next.js and Supabase, including the PKCE flow, callback handling, and production configuration that handles independent founders.

Why Google OAuth for Healthcare

Healthcare patients already have too many accounts. Insurance portals, patient portals for each provider, pharmacy accounts, lab results portals — the last thing they want is another password to remember. Google OAuth lets patients sign in with one click using an account they already have open in their browser.

The conversion impact is massive. Our A/B test showed that removing the email/password form and leading with Google OAuth increased sign-up conversion by 34%. When we added the option back as a secondary method, the Google OAuth rate stabilized at 72% of all sign-ups.

Supabase Auth Google Provider Setup

Supabase supports Google OAuth natively through its Auth configuration. In the Supabase dashboard, enable the Google provider, enter the OAuth client ID and secret from Google Cloud Console, and configure the redirect URL. Supabase handles the entire OAuth flow — token exchange, user creation, session management — without custom server code.

The Supabase client library provides a signInWithOAuth method that redirects the user to Google's consent screen. After the user approves, Google redirects back to your callback URL with an authorization code. Supabase exchanges this code for tokens, creates or updates the user record, and establishes a session.

PKCE Flow for Security

We use the PKCE (Proof Key for Code Exchange) flow instead of the implicit flow. PKCE prevents authorization code interception attacks by binding the code to the client that requested it. When initiating the OAuth flow, the client generates a random code verifier and sends its hash (code challenge) with the authorization request. The callback includes the original verifier, which Supabase validates against the hash.

Next.js handles PKCE automatically through the @supabase/ssr package. The code verifier is stored in an HTTP-only cookie during the OAuth redirect, ensuring it survives the page navigation to Google and back. This is more secure than storing it in localStorage or sessionStorage, which are vulnerable to XSS attacks.

The Auth Callback Route

The callback route is a critical piece. After Google redirects back with the authorization code, our /auth/callback route handler exchanges the code for a session using Supabase's exchangeCodeForSession method. This server-side handler runs on the server, keeping the code exchange secure and setting HTTP-only session cookies.

Error handling in the callback is important for user experience. If the code exchange fails — expired code, invalid state, network error — we redirect to the sign-in page with an error parameter that displays a user-friendly message. We never show raw OAuth errors to patients.

Session Management

Supabase sessions use two tokens: an access token (JWT) with a 1-hour expiration and a refresh token with a 7-day expiration. The @supabase/ssr package manages these tokens in HTTP-only cookies, automatically refreshing the access token when it expires. Users stay signed in for up to 7 days without re-authenticating.

Our Next.js middleware reads the session cookie on every request to protected routes. If the access token is expired, middleware calls supabase.auth.getSession() which automatically uses the refresh token to get a new access token. This happens transparently — the user never sees a loading state or sign-in redirect unless their refresh token has also expired.

Production Configuration

In production, several additional configurations are critical. The Google OAuth consent screen must be set to "production" mode to allow any Google account to sign in (vs testing mode which limits to test users). The authorized redirect URI must exactly match your production callback URL including https and www if applicable. Rate limiting on the callback endpoint prevents token exchange abuse.

We also configure the OAuth scope to request only the minimum information needed: email and profile. We do not request access to Google Drive, Calendar, or any other Google service. The minimum necessary principle from HIPAA applies to OAuth scopes too — only request what you actually need.

User Profile Enrichment

When a new user signs in via Google OAuth, we receive their name, email, and profile photo URL. We store these in the Supabase user metadata and use them to pre-populate the patient profile. This reduces the profile completion steps from five fields to two (adding phone number and location preference), further reducing onboarding friction.

Frequently Asked Questions

How do you set up Google OAuth with Next.js and Supabase?

Enable the Google provider in Supabase dashboard with your OAuth client ID/secret from Google Cloud Console. Use @supabase/ssr for PKCE flow with HTTP-only cookie storage. Create an /auth/callback route that exchanges the authorization code for a session. The Supabase client library handles the OAuth redirect and token exchange.

What is PKCE flow and why use it for OAuth?

PKCE (Proof Key for Code Exchange) prevents authorization code interception by binding the code to the requesting client. The client generates a code verifier, sends its hash with the auth request, and includes the verifier during code exchange. This prevents attackers who intercept the code from using it.

How does Google OAuth improve healthcare app sign-up rates?

Google OAuth eliminates password creation friction. OpenMyPro saw a 34% increase in sign-up conversion by leading with Google OAuth. 72% of all sign-ups use Google because patients already have Google accounts open in their browsers and do not want another password.

Ready to find the right AI tool? Our AI matching finds it in 33 seconds.

Independent engineering. 190+ providers. Book healthcare in 33 seconds — not 26 days.

Ex-Amazon Engineer · Healthcare Innovation

No card charged today · AI-powered matching · 33-second booking

Get Founder Insights Weekly

Startup lessons, technical deep dives, and behind-the-scenes of building a 14-platform ecosystem. No spam.

Find your provider in 33 seconds. Start free today.

Independent engineering. 190+ providers. Book healthcare in 33 seconds — not 26 days.

Ex-Amazon Engineer · Healthcare Innovation

No card charged today · Cancel anytime · strong LTV/CAC

Noizz helps you discover and compare the best new products and tools. Try it free →

Build your professional portfolio

Free to get started. No card charged today.

Get Started

Tools We Recommend

Find healthcare providers

AI-powered matching. Book a provider in 33 seconds.

Try OpenMyPro

Discover trending brands

Product Hunt-style brand discovery with AI insights.

Explore Brands

AI-Powered Healthcare Tech

Multi-platform marketplace network — 13 production platforms across healthcare, brand discovery, and content marketing.

Learn More

Ready to work together? Get in touch or explore our platforms.

More tools by the same team

Find Healthcare Providers Instantly

AI-powered matching. Book a therapist, trainer, or nutritionist in 33 seconds.

Try OpenMyPro Free

Work With Me

Get updates on new projects, tools, and tech insights.

No spam. Unsubscribe anytime.

Visit Blossend.com →

Explore the full portfolio of independent AI tools and editorial properties at blossend.com.