/supabase-tutorials

How to use third-party OAuth providers with Supabase?

Integrate third-party OAuth providers with Supabase. Learn to configure projects, set environment variables, and implement user sign-in easily.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to use third-party OAuth providers with Supabase?

 
Step 1: Set Up Supabase Project
 

Before integrating third-party OAuth providers, you need to set up a Supabase project.

  • Go to the Supabase website and sign into your account.
  • Click on the "New Project" button on the dashboard.
  • Enter the project details like "Project Name", "Organization", and choose the desired "Region".
  • Click "Create New Project".

Once the project is created, you'll have access to your project's API keys and other related details.

 
Step 2: Enable and Configure OAuth Providers
 

To use OAuth providers like Google, GitHub, etc., you need to configure them in your Supabase dashboard.

  • Navigate to the "Authentication" tab in your Supabase project.
  • Click on "Providers".
  • You will see a list of available providers like Google, GitHub, GitLab, Bitbucket, etc.
  • Select the provider you want to configure, for example, Google.

For each provider, you will need to:

  • Obtain the client ID and secret from the respective platform's developer console.
  • Enter these credentials into the Supabase dashboard.

For Google OAuth:

  • Go to the Google Cloud Console.
  • Create a new project or select an existing one.
  • Navigate to "APIs & Services" > "Credentials".
  • Click "Create Credentials" > "OAuth Client ID".
  • Configure the consent screen and then create the OAuth client ID.
  • Save the client ID and client secret and enter them into the Supabase settings for Google.

Repeat similar steps for other providers and enter the client ID and secret into Supabase.

 
Step 3: Modify Your .env File
 

To connect your application with Supabase using OAuth providers, you need to update your environment configuration.

  • Add your Supabase endpoint, anon, and service role keys to your .env file.
  • You will also need to set the redirect URL which OAuth providers will use to redirect users after a successful authentication.

Here's an example portion of a .env file:


SUPABASE\_URL=https://your-supabase-url.supabase.co
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE\_KEY=your-service-role-key
REDIRECT\_URL=http://yourapp.com/auth/callback

Ensure these values are aligned with your Supabase and OAuth provider configurations.

 
Step 4: Integrate Supabase Client in Your Application
 

You'll need to set up Supabase in your application.

  • First, install the Supabase JavaScript client:

npm install @supabase/supabase-js
  • Import and initialize the client in your application using the keys from your .env file:

import { createClient } from '@supabase/supabase-js'

const supabaseUrl = process.env.SUPABASE\_URL
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY

const supabase = createClient(supabaseUrl, supabaseAnonKey)

 
Step 5: Implement the OAuth Sign-In Flow
 

Your app will now execute the OAuth login flow using the configured provider.

  • Create a function to handle user sign-in with an OAuth provider. Supabase provides a simple method for this:

async function signInWithProvider(provider) {
  const { user, session, error } = await supabase.auth.signIn({
    provider: provider
  }, {
    redirectTo: process.env.REDIRECT\_URL
  });

  if (error) {
    console.error("Error signing in with provider: ", error.message)
  } else {
    console.log("User signed in: ", user);
  }
}
  • Call this function with the desired provider, e.g., "google", "github", etc.

signInWithProvider('google');

 
Step 6: Handle OAuth Callback
 

Set up a route in your application to handle the OAuth callback.

  • Customize your application to capture and handle the OAuth response.
  • Use the token returned in the callback to fetch user data or manage session information in context to your application needs.

For example, if you are using a framework like React, configure your route:


useEffect(() => {
  supabase.auth.onAuthStateChange((event, session) => {
    if (session) {
      // User is signed in
    } else if (event === 'SIGNED\_OUT') {
      // User is signed out
    }
  });
  
  supabase.auth.getSessionFromUrl({ storeSession: true });
}, []);

This ensures your app properly manages user sessions and handles sign-in and sign-out actions effectively.

Remember to test the OAuth flow thoroughly to make sure users are able to authenticate without issues and that their session is managed correctly within your application.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022