Supabase "AuthApiError: Invalid login credentials" when the user and password look right

By AI App Medic · Updated · Supabase · Auth · Lovable · Login

Someone types an email and password into your login form and gets this back, often while insisting the password is right. Sometimes that someone is you, with a test account you just created:

AuthApiError: Invalid login credentials
    at handleError (@supabase/auth-js)
status: 400
code: "invalid_credentials"

In the Network tab the failing request is a POST to https://<project-ref>.supabase.co/auth/v1/token?grant_type=password and the response body is:

{
  "code": 400,
  "error_code": "invalid_credentials",
  "msg": "Invalid login credentials"
}

The important thing to know is that Supabase Auth sends this exact response in three different situations: no user has that email, the user exists but has no password stored, or the password does not match. It hides which one happened so that your login form cannot be used to check who has an account. That is good for your users, but it means the message alone tells you nothing. You have to look at the account from the database side.

In apps built with Lovable, Bolt, Cursor or v0 we see the same handful of causes again and again: the app points at a different Supabase project than the one being checked, the account was created with Google or a magic link, or the AI assistant "seeded" users with a SQL insert that stores the password in a form Supabase cannot read.

Step 1: make sure you are looking at the right project

This one catches developers more often than end users. Open DevTools, go to the Network tab, sign in and click the token?grant_type=password request. Note the host, for example abcdwxyz.supabase.co. Now compare it with the URL of the project open in your Supabase dashboard (Project Settings, Data API).

They differ more often than you would expect:

To see what the built app really uses, log it once in the browser console (remove it afterwards):

// Vite (Lovable, Bolt)
console.log(import.meta.env.VITE_SUPABASE_URL);

// Next.js (Cursor, v0)
console.log(process.env.NEXT_PUBLIC_SUPABASE_URL);

If the value is not the project you expected, fix the environment variables where the app is built and redeploy. Environment variables undefined on Vercel and Bolt env variables undefined on Netlify cover why a changed variable only takes effect after a new build.

Step 2: look up the account in auth.users

Every account is a row in auth.users, and the sign-in methods it can use are rows in auth.identities. Open the SQL editor in the right project and run this with the email the person typed. It only reads, and it never shows a password, only whether one is stored:

select
  u.id,
  u.email,
  u.created_at,
  u.email_confirmed_at,
  u.last_sign_in_at,
  coalesce(u.encrypted_password, '') <> '' as has_password,
  left(u.encrypted_password, 4) as hash_prefix,
  array_agg(i.provider) as providers
from auth.users u
left join auth.identities i on i.user_id = u.id
where lower(u.email) = lower('person@example.com')
group by u.id;

Read the result like this:

Step 3: repair users that were created with SQL

When you ask an AI assistant for "a few test users" or "an admin account", it often writes a migration like this:

-- what the assistant wrote: this account can never sign in
insert into auth.users (id, email, encrypted_password, role, aud)
values (gen_random_uuid(), 'admin@example.com', 'Admin123!', 'authenticated', 'authenticated');

Supabase Auth compares the typed password with a bcrypt hash. A plain string, an MD5 or a SHA-256 value in encrypted_password never matches, so the result is Invalid login credentials forever. Rows inserted this way often have other problems too: no row in auth.identities, and NULL in token columns that Auth expects to be empty strings, which shows up as a 500 with Database error querying schema instead.

The cleanest fix is to delete those rows and recreate the users through the Auth API, which fills in every column correctly. From a server script or the Node REPL, with the service role key (never in browser code):

import { createClient } from "@supabase/supabase-js";

const admin = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY, {
  auth: { persistSession: false, autoRefreshToken: false },
});

const { data, error } = await admin.auth.admin.createUser({
  email: "admin@example.com",
  password: "a-long-unique-password",
  email_confirm: true,
});
console.log(error ?? data.user.id);

You can also add users in the dashboard under Authentication, Users, Add user. If other tables already reference the old user id and you would rather keep the row, you can replace just the hash. pgcrypto lives in the extensions schema on Supabase:

update auth.users
set encrypted_password = extensions.crypt('a-long-unique-password', extensions.gen_salt('bf'))
where lower(email) = lower('admin@example.com');

Then remove the seed from your migrations, or rewrite it to call the admin API, so the next database reset or new environment does not bring the broken users back. If the service role key ended up in a seed file that was committed or shipped to the browser, rotate it; Supabase service role key exposed explains how.

Step 4: check what the form really sends

If the account looks fine and you are sure of the password, look at the request payload in the Network tab. Generated login forms have a few common bugs:

A minimal, correct handler looks like this:

async function handleLogin(event) {
  event.preventDefault();
  const form = new FormData(event.currentTarget);
  const email = String(form.get("email") || "").trim();
  const password = String(form.get("password") || "");

  const { data, error } = await supabase.auth.signInWithPassword({ email, password });
  if (error) {
    showLoginError(error);
    return;
  }
  // data.session is set here; redirect to the app
}

Step 5: give every user a way back in

Handle errors by error.code, not by matching the message text, and keep the message neutral for the cases Supabase deliberately hides:

function showLoginError(error) {
  switch (error.code) {
    case "invalid_credentials":
      setMessage("Email or password is incorrect. You can reset your password, or use the button you signed up with.");
      break;
    case "email_not_confirmed":
      setMessage("Please confirm your email first. We can send the link again.");
      setShowResend(true);
      break;
    case "over_request_rate_limit":
      setMessage("Too many attempts. Wait a few minutes and try again.");
      break;
    default:
      setMessage("Sign-in failed. Please try again.");
  }
}

Then make sure the two escape routes actually work:

Do not add an endpoint that tells the browser whether an email exists so the form can say "no account found". That brings back exactly the leak Supabase is preventing, and attackers do look for it.

If the error is "Email not confirmed" instead

That message, with code email_not_confirmed, means the password was correct and Confirm email is on, but the user never clicked the confirmation link. Offer a resend button:

await supabase.auth.resend({ type: "signup", email });

If the confirmation emails never arrive, the default Supabase mailer is the usual reason; Supabase auth emails not sending walks through setting up SMTP. Turning off Confirm email does not confirm accounts that already exist, and it lets anyone sign up with an address they do not own, so fix delivery rather than switching it off. To confirm a single account by hand, use the dashboard or the admin API with email_confirm: true.

Too many attempts

While testing you may hit the sign-in rate limit, which returns HTTP 429 with over_request_rate_limit rather than Invalid login credentials. The limits are under Authentication, Rate Limits. Raise them for a load test if you need to, but keep a limit in production: it is what slows down password guessing. Email limits are a separate setting, covered in email rate limit exceeded.

Prevention

Create users only through Supabase Auth: the sign-up form, the dashboard or auth.admin.createUser() in a server script. Never write to auth.users from a migration. Keep one clearly named Supabase project per environment and print the project ref somewhere visible in non-production builds, so nobody tests against the wrong one. Handle error.code on every auth call, ship a working password reset before launch, and show the same sign-in options on the login page as on the sign-up page. Before launch, test four accounts end to end: a confirmed password user, an unconfirmed one, a Google user and a magic link user.

FAQ

What does Invalid login credentials mean in Supabase?

It is the 400 response with error code invalid_credentials from the password grant. Supabase returns the same message when no user has that email, when the user has no password stored, and when the password does not match. It does this on purpose so the login form cannot be used to find out who has an account.

Why do I get Invalid login credentials when the password is correct?

Usually one of the other two cases applies. The app may be talking to a different Supabase project than the one you checked, the account may have been created with Google or a magic link and has no password you know, or the user was inserted with SQL and the stored hash is not a bcrypt hash.

Is Invalid login credentials caused by an unconfirmed email?

No. Supabase checks the password first. If the password is right and the email is not confirmed you get a different error, Email not confirmed, with the code email_not_confirmed.

How do I create test users that can actually sign in?

Use the dashboard (Authentication, Users, Add user) or call auth.admin.createUser({ email, password, email_confirm: true }) from a server script with the service role key. Do not insert rows into auth.users with a hand-written SQL seed unless you hash the password with crypt() and gen_salt('bf') and also create the matching auth.identities row.

Can a user who signed up with Google also log in with a password?

Only after a password is set for the account. The simplest way is the normal password reset flow: the user requests a reset email, follows the link and sets a password with updateUser. Until then, every password attempt for that email returns Invalid login credentials.

Should my login form say the email does not exist?

No. Keep one neutral message such as Email or password is incorrect, and offer password reset and social login. Telling users which part was wrong lets anyone test which emails are registered in your app.

Related guides