Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 31 ยท Authentication

Email and Password Sign-In

Sign users up and in with email and password in Firebase Auth and Supabase Auth: verification, password reset, sessions, errors, plus a runnable stand-in.

Email and password is the oldest sign-in method and still the one most apps start with. Both platforms handle the risky parts for you: they hash passwords, send verification and reset emails, limit repeated attempts, and keep a session in the browser. Your code only calls a few SDK functions and reacts to the result.

In this lesson you will see the real calls for Firebase Authentication and Supabase Auth (read-only, they need a project), and then run a stand-in auth service that shows what happens inside: salted password hashing and session tokens. The stand-in is ours and is much simpler than the real services.

What happens when a user signs up

Sign up in four steps

Send details Email and password go over HTTPS.
Hash The service stores a salted hash, never the password.
Verify A link proves the email is real.
Session The SDK stores tokens and signs you in.

You never see or store the password yourself.

1 / 2

Firebase Authentication

The modular SDK uses functions that take the auth object you created in Creating Projects and Using the SDKs.

JavaScript
// Firebase (read-only)
import {
  createUserWithEmailAndPassword,
  signInWithEmailAndPassword,
  sendEmailVerification,
  sendPasswordResetEmail,
  onAuthStateChanged,
  signOut,
} from "firebase/auth";

// Sign up
try {
  const cred = await createUserWithEmailAndPassword(auth, email, password);
  await sendEmailVerification(cred.user);
} catch (error) {
  console.log(error.code);   // for example "auth/email-already-in-use"
}

// Sign in
await signInWithEmailAndPassword(auth, email, password);

// React to the signed-in state (also fires on page load)
const unsubscribe = onAuthStateChanged(auth, (user) => {
  if (user) {
    console.log(user.uid, user.email, user.emailVerified);
  } else {
    console.log("signed out");
  }
});

await sendPasswordResetEmail(auth, email);
await signOut(auth);

Errors carry a code string such as auth/email-already-in-use, auth/invalid-credential, auth/weak-password and auth/too-many-requests. Match on the code, not on the message text, and show friendly wording of your own.

Supabase Auth

supabase-js returns an object with data and error instead of throwing.

JavaScript
// Supabase (read-only)
const { data, error } = await supabase.auth.signUp({
  email,
  password,
  options: { emailRedirectTo: "https://example.com/welcome" },
});
if (error) {
  console.log(error.code, error.message);   // for example "user_already_exists"
}

const { data: login, error: loginError } = await supabase.auth.signInWithPassword({
  email,
  password,
});

// Subscribe to changes of the session
const { data: sub } = supabase.auth.onAuthStateChange((event, session) => {
  console.log(event, session?.user?.id);     // SIGNED_IN, SIGNED_OUT, TOKEN_REFRESHED ...
});
sub.subscription.unsubscribe();

await supabase.auth.resetPasswordForEmail(email, { redirectTo: "https://example.com/reset" });
await supabase.auth.updateUser({ password: newPassword });  // on the reset page
await supabase.auth.signOut();

Two details differ from Firebase. By default Supabase projects require email confirmation: signUp succeeds, but there is no session until the user clicks the link. And to avoid revealing which emails are registered, signUp for an existing confirmed email may return a result that looks like success. Check the current behavior of your project settings in the dashboard.

Email verification and password reset

Both flows are the same idea: the service emails a link containing a short-lived, single-use token. Clicking it proves the person controls that mailbox.

  • Verification marks the user as confirmed (emailVerified in Firebase, email_confirmed_at in Supabase). Use it to gate sensitive features, not just to collect clean addresses.
  • Password reset sends a link to a page of yours where the user enters a new password. The old sessions can be ended afterward.

Never write your own "forgot password" by emailing a password or a guessable link. Let the platform generate the token.

Sessions and persistence

After sign-in the SDK holds a short-lived access token (an ID token in Firebase) and a long-lived refresh token, and renews the first one in the background. Where they are kept is the persistence setting.

  • Firebase on the web defaults to browserLocalPersistence (survives closing the browser). You can choose browserSessionPersistence (until the tab closes) or inMemoryPersistence with setPersistence.
  • Supabase stores the session in localStorage by default and restores it on load; server-rendering frameworks use cookies through helper packages.

Browser storage can be read by any script on your page, so an XSS bug exposes the session. That is why Content Security Policy and careful dependencies matter. Tokens are explained in the next lesson, OAuth Providers, Magic Links and JWTs.

A stand-in auth service

The real services run on servers we cannot reach. The stand-in below runs in Node on this site and mirrors the main ideas: a random salt per user, a slow hash (scrypt, from node:crypto) compared in constant time, one generic error for wrong email or wrong password, and sessions that are random tokens with an expiry. For deeper background see Passwords, scrypt and Tokens.

Node.js Stand-in: sign-up, sign-in and sessions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Note how signIn returns the same invalid-credential error for a wrong password and an unknown email. Real services do the same, so attackers cannot use the form to find out which emails have accounts. The stand-in is also missing things the real services provide: rate limiting, email delivery, refresh tokens, and password breach checks.

The next example shows why storing passwords with a plain fast hash is a bad idea, by comparing how many guesses per second a thief could try against each.

Node.js Stand-in: why slow hashes matter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The exact numbers depend on the machine, so the program prints a comparison that holds everywhere instead of raw speeds. Firebase and Supabase both use deliberately slow, salted hashing, so a leaked database is far harder to crack.

Practical tips

  • Require verification for what matters. Check emailVerified (Firebase) or email_confirmed_at (Supabase) before letting users post publicly or change billing.
  • Use the platform's password rules. Set a minimum length in the console or dashboard; length matters more than odd symbol rules.
  • Handle errors by code. Map auth/invalid-credential or invalid_credentials to one friendly message.
  • Do not trust the client for roles. A user's role belongs in claims or a protected table. See Users, Roles and Custom Claims.
  • Protect sign-in with CAPTCHA or App Check when bots appear, and keep the built-in rate limits on.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which function signs a user in with a password in Supabase JS?

    supabase-js uses auth.signInWithPassword. signInWithEmailAndPassword is the Firebase function.
  2. 2. Why should sign-in return the same error for an unknown email and a wrong password?

    Different messages would let anyone test whether an address has an account.
  3. 3. How do you decide how to react to a Firebase sign-in error?

    Messages can change and may be localized. Codes are stable identifiers.
  4. 4. What does the service store instead of the password?

    A salted slow hash cannot be reversed and is expensive to guess, even after a data leak.

Summary

  • Firebase uses functions like createUserWithEmailAndPassword and signInWithEmailAndPassword; Supabase uses auth.signUp and auth.signInWithPassword.
  • Verification and reset emails carry single-use tokens generated by the platform.
  • The SDK keeps an access token and a refresh token and renews them for you; persistence decides where they live.
  • Services store salted slow hashes and return one generic error for failed sign-ins.
  • Handle errors by code and gate sensitive features on verified email.

Next: OAuth Providers, Magic Links and JWTs