Secure Your Next.js App: Email & Google Authentication with Supabase, PostgreSQL RLS, and Triggers - Part 3
Published on March 15, 2025
This article might not be well formatted due to markdown render issues.
Let us see how to enhance our web app's security and user experience by integrating Google authentication using Supabase. This guide will walk you through creating a Google Cloud project, obtaining client credentials, and configuring them in Supabase for seamless Google sign-in functionality.
đź”—Â Check out the full code for this series here.
Google Cloud is a comprehensive suite of cloud computing services that includes Google Cloud Platform (GCP), Google Workspace, and more. GCP provides a robust infrastructure for building, deploying, and managing web applications, leveraging services such as computing, storage, and machine learning. A Google Cloud project is a top-level entity that organizes resources and services, allowing users to manage access, billing, and resource allocation efficiently.
Ignore this step if you want to integrate Google login in an already existing project.
💡 Tip: If you don’t find these settings, then just search for “OAuth consent screen” in the search bar at the top.
Now we are ready to create a new OAuth Client that will handle the signup and login in our application.
Now we are ready to set up the routes in our NextJS application to perform the Google Authentication.
Now all we need is to add the function that will handle the Google signup and Login for us.
Add the signInWithGoogle
function to the component below and use this function on the Google sign in button.
/app/signup/page.ts
import { useSearchParams } from "next/navigation";
import { createClient } from "@/utils/supabase/client";
// rest of the code
...
...
// rest of the code
const searchParams = useSearchParams();
const next = searchParams.get("next");
const supabase = createClient();
async function signInWithGoogle() {
try {
const { error } = await supabase.auth.signInWithOAuth({
provider: "google",
options: {
redirectTo: `${window.location.origin}/auth/callback${
next ? `?next=${encodeURIComponent(next)}` : ""
}`,
},
});
if (error) {
throw error;
}
} catch (error) {
alert("There was an error logging in with Google."),
console.error(error);
}
}
// rest of the code
// ...
// ...
// rest of the code
<Button
type="button"
variant="outline"
className="w-full"
onClick={() => signInWithGoogle()}
>
<Mail size={16} className="mr-2" />
Sign up with Google
</Button>
/app/login/page.tsx
Test out the Google Login/Signup flow.
Note: Supabase will take care of the Email and Google integration if you Sign Up with your Gmail address.
đź”—Â Check out the full code for this series here.
If you found this article useful, like, comment, and share, or just buy me a coffee?