Next.js Supabase adding a GitHub login

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Now that we have our basic Supabase auth setup with our magic link login let's see how we can add GitHub as a login provider.
That way, users can also sign up by using their GitHub account.
The end result will be the following flow:

The first thing we need to do is create an app on GitHub to get the API token from there.
Go to the GitHub developer center and create a new OAuth app.

You'll need to fill out the details on the next step. To find your Authorization callback URL visit the Supabase interface.
The URL we need is the following one:

However, you'll have to add the following to the end of this URL: /auth/v1/callback.
My end URL looks like this:
https://texcivmahyzsgwyjdvvj.supabase.co/auth/v1/callback
Finish creating this OAuth app in GitHub, now you should get a page where you have to generate a new secret.

Once you generate the secret, note this down as well as the client id.
And then head back over to Supabase. Click the Authentication menu and visit the settings.
Here you can enable all separate providers. In our case, we want GitHub.

Fill out the client id and secret as you just retrieved them from GitHub.
Now it's finally time to open our Next.js application. We'll be using the one we made before. Which you can find on GitHub.
Open up the components/Login.js component.
Let's start by adding the function that will authenticate the user.
We can make use of the loading state we created before.
const handleGitHubLogin = async () => {
try {
setLoading(true);
const { error } = await supabase.auth.signIn({
provider: 'github',
});
if (error) throw error;
} catch (error) {
alert(error.error_description || error.message);
} finally {
setLoading(false);
}
};
And now, all we need to do is render the button in our HTML.
<button
className='mt-4 p-2 pl-5 pr-5 bg-blue-500 text-gray-100 text-lg rounded-lg focus:border-4 border-blue-300'
onClick={() => handleGitHubLogin()}
disabled={loading}
>
{loading ? 'Logging in' : 'Login with GitHub'}
</button>
And that's it! We can now log in through GitHub and still enrich our profile.
You can download the complete code on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter