Supabase Authentication With supabase-js v2
Supabase Auth signs people in with email and password, one-time magic links, phone codes or OAuth providers such as GitHub, all through the supabase.auth object in supabase-js. Each sign-in yields a session whose token reaches your database, where row level security policies compare auth.uid() with a user_id column to decide what that person may read.
| Method | supabase-js v2 call | Good for |
|---|---|---|
| Email and password | auth.signUp / auth.signInWithPassword | Classic accounts |
| Magic link | auth.signInWithOtp({ email }) | No passwords to forget |
| SMS or WhatsApp code | auth.signInWithOtp({ phone }) | Mobile-first products |
| OAuth provider | auth.signInWithOAuth({ provider }) | One-click sign-in with an existing account |
| Sign out | auth.signOut() | Ending the session |
Updated
Auth is the part of a Supabase project people most often wire up wrong: the sign-in works, but the data is wide open or the server trusts a cookie it should not. Pick a method below to get its code, then follow the policies section so each person only sees their own rows.
Code for each sign-in method
- Email and password
- Magic link
- OAuth (GitHub)
- Session listener and sign-out
Email and password
Create the account, then sign in. With confirmation on, data.session is null until the email link is clicked.
import { createClient } from "@supabase/supabase-js";
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
const { data, error } = await supabase.auth.signUp({
email: "[email protected]",
password: "a-long-unique-passphrase",
options: { emailRedirectTo: "https://example.com/welcome" },
});
const { data: signedIn, error: signInError } = await supabase.auth.signInWithPassword({
email: "[email protected]",
password: "a-long-unique-passphrase",
});Policies that use auth.uid()
Each person reads and writes only their own notes
Run in the SQL editor. The default fills user_id from the token, and with check stops anyone inserting rows for another user.
create table public.notes (
id bigint generated always as identity primary key,
user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
body text not null
);
alter table public.notes enable row level security;
create policy "read own notes" on public.notes
for select to authenticated
using ((select auth.uid()) = user_id);
create policy "add own notes" on public.notes
for insert to authenticated
with check ((select auth.uid()) = user_id);
create policy "edit own notes" on public.notes
for update to authenticated
using ((select auth.uid()) = user_id)
with check ((select auth.uid()) = user_id);
create policy "delete own notes" on public.notes
for delete to authenticated
using ((select auth.uid()) = user_id);
Sessions and tokens
After sign-in, supabase-js stores a short-lived access token and a refresh token, and refreshes the access token automatically. Every query you make through the same client sends the access token, which is how Postgres learns the user's ID.
In server-rendered apps the session lives in cookies instead. The Next.js authentication guide on this site covers that wiring, so this page stays with the client library and the database rules.
Row level security is the real lock
Hiding a button in the interface does not protect data, since anyone can call your API with the public key. Policies run inside Postgres on every request, which makes them the place where ownership is enforced.
Write one policy per action. A select policy uses using, while insert needs with check so people cannot create rows owned by someone else, and update usually needs both.
How it works
- Pick a sign-in method in the code picker and copy the snippet into your app.
- Turn the matching provider on in the Supabase dashboard and add your site's address to the allowed redirect URLs.
- Listen for auth events so your interface updates when someone signs in or out.
- Enable row level security on every table that holds personal data and add policies that use auth.uid().
Tips
- With email confirmation turned on, signUp returns a user but a null session until the link is clicked, so show a check-your-inbox message rather than a dashboard.
- Wrap the function as (select auth.uid()) inside policies. Postgres can then evaluate it once per statement instead of once per row.
- auth.uid() is null for requests without a signed-in user, so a policy comparing it with user_id simply matches nothing for anonymous visitors.
- On the server, do not trust getSession() read from cookies; call getUser() or getClaims() to verify who is asking.
- The publishable (anon) key is safe in browsers because policies limit it. A secret or service role key skips row level security and must stay on the server.
Frequently asked questions
Is Supabase magic link the same as OTP?
Both start from signInWithOtp. For email, the message carries a link when the template uses the confirmation URL and a code when it uses the token. Phone sign-in only supports codes, by SMS or WhatsApp.
How do I add Google or GitHub sign-in?
Enable the provider in the dashboard with the client ID and secret from that provider, then call signInWithOAuth with its name and an optional redirectTo address. The person returns to your site already signed in.
How do I know when a user signs in or out?
Register onAuthStateChange. It reports events including INITIAL_SESSION, SIGNED_IN, SIGNED_OUT, TOKEN_REFRESHED, USER_UPDATED and PASSWORD_RECOVERY, and returns a subscription you unsubscribe from when the component unmounts.
Why can signed-in users see nothing after I enabled RLS?
Once row level security is on, a table denies everything until a policy allows it. Add a select policy for the authenticated role that compares (select auth.uid()) with the owner column.
Should I use Supabase Auth or Firebase Auth?
Both cover the common methods. The bigger difference is the database underneath; our Supabase vs Firebase comparison goes through that choice in detail.
Related: Supabase vs Firebase, Next.js authentication, Accept payments on a website.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $20 a month.
More in Guides
- Supabase CLI: Install It and Learn the Commands You Will Use
- Supabase MCP Server: Connect Safely in Five Minutes
- Supabase Pricing: What Each Plan Costs in October 2026
- Supabase vs Firebase for Web Apps
- Svelte 5 Runes, Side by Side with Svelte 4
- Svelte vs React: One Component, Two Ways
- SVG Animation: 20 Live Examples With Code
- Tailwind Animations: Built-in Classes and Custom Keyframes
Related guides
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App