Login and sign-up in 10 minutes with Supabase and Next.js
If someone asked you to build a complete authentication flow in a single day (login, sign-up with email confirmation, password recovery and pages only logged-in users can see), what would you do? Before calling on higher powers: with Next.js and Supabase it takes a few minutes.
In this article I'll show you how, step by step, starting from a ready-made template.
Vercel's Supabase Starter template
▶ Watch from 00:32The starting point is Supabase Starter, a template provided by Vercel. It uses the same technologies I use in my projects: Next.js with the App Router and middleware, Supabase, TypeScript, Tailwind and shadcn/ui. And it already does pretty much everything we need.

There's also a Deploy button that publishes it straight to your Vercel account. Here, though, we'll run it locally to see how it works.
Creating the project locally
▶ Watch from 01:03From the terminal, run the command from the guide. It asks for the app name (I called mine supabase-login) and downloads everything, with Tailwind and shadcn/ui already installed.
npx create-next-app -e with-supabase
cd supabase-loginThe project folder contains a .env.example file: as the guide says, rename it to .env.local. Inside are the two keys to fill in with your Supabase details.
NEXT_PUBLIC_SUPABASE_URL=[INSERT SUPABASE PROJECT URL]
NEXT_PUBLIC_SUPABASE_ANON_KEY=[INSERT SUPABASE PROJECT API ANON KEY]Creating the project on Supabase
▶ Watch from 02:03On Supabase, once you've signed up (it's free), create a new project. I gave it the same name as the code project, supabase-login. Let Supabase generate the database password and save it somewhere: you'll need it. For the region I left Central EU (Frankfurt).

While Supabase sets up the project, go to Project Settings → API and copy the project URL and the anon key into your .env.local.
Starting the app and signing up the first user
▶ Watch from 03:38Last step of the guide: start the development server.
npm run devOn localhost:3000 the app already works, and it even walks you through creating the first user.

There are Sign up and Sign in pages, and Forgot password is ready too. I signed up with an email and password, the confirmation email arrived right away and, after clicking the link, I landed on the protected page.
How the code is organised
▶ Watch from 04:38Inside app there's a (auth-pages) folder. The parentheses group routes without changing the URL: this is where all the authentication routes live, namely sign-in, sign-up and forgot-password. Under protected are the pages only logged-in users can see.
If you sign out and try to open /protected, you're sent back to sign-in. When you sign in again, you go straight back to the protected page.
The protected page also suggests next steps for a fuller app, with ready-made code. For example, to create a notes table with some sample data:
create table notes (
id bigserial primary key,
title text
);
insert into notes(title)
values
('Today I created a Supabase project.'),
('I added some data and queried it from Next.js.'),
('It was awesome!');Right below are examples for reading that data from Next.js, both server-side and client-side. It's a quick, detailed guide with no frills.
Changing the redirect after sign-up
▶ Watch from 06:10Since it's all code, you can change any behaviour. For example, after confirming their sign-up the user is sent to /protected, but maybe you want them to stay on the home page. The place to change is the app/auth/callback/route.ts route:
import { createClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
export async function GET(request: Request) {
// The `/auth/callback` route is required for the server-side auth flow implemented
// by the SSR package. It exchanges an auth code for the user's session.
// https://supabase.com/docs/guides/auth/server-side/nextjs
const requestUrl = new URL(request.url);
const code = requestUrl.searchParams.get("code");
const origin = requestUrl.origin;
const redirectTo = requestUrl.searchParams.get("redirect_to")?.toString();
if (code) {
const supabase = createClient();
await supabase.auth.exchangeCodeForSession(code);
}
if (redirectTo) {
return NextResponse.redirect(`${origin}${redirectTo}`);
}
// URL to redirect to after sign up process completes
return NextResponse.redirect(`${origin}/`); // was `${origin}/protected`
}Change the last line from /protected to / and the user ends up on the home page.
Watch out for emails: you need a custom SMTP
▶ Watch from 07:05One thing I recommend checking right away is the Enable Custom SMTP setting, under Project Settings → Authentication.

Since 26 September 2024, to fight spam, emails from Supabase's built-in service only reach members of your organisation. I signed up with an email from my organisation, so the confirmation arrived. With someone else's email it wouldn't have, and that user couldn't have completed their sign-up.
That's fine during development but, as Supabase itself says, the built-in service isn't meant for production: before going live you need to set up your own SMTP.
Login with Google, Apple and other providers
▶ Watch from 08:15The Authentication section also shows your registered users and the list of login providers: besides email and password you can enable Google, Apple, GitHub and many more. Each one needs to be configured by following its own short guide.

In just a few steps you have a solid authentication system, ready to customise. If you need to move this fast on a real product, that's exactly the kind of work I do when I build an MVP.
