Thuta Learning
IntermediateDevOps & ToolsIntermediate

Logging in with email/password

Firebase TutorialLesson 8

Logging in with email/password

To log an existing user in with Firebase Auth, use signInWithEmailAndPassword.

Code Example

javascript
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth';

const auth = getAuth();

async function login(email, password) {
  try {
    const userCredential = await signInWithEmailAndPassword(auth, email, password);
    console.log('Logged in:', userCredential.user.email);
  } catch (error) {
    console.log('Login failed:', error.code);
  }
}

login('student@example.com', 'StrongPass123');

What to keep in mind

Once login succeeds, Firebase keeps track of the session for you. To pick the login state back up even after a page refresh, use onAuthStateChanged.

Expected outputLogged in: student@example.com

Security note

Don't store the password in localStorage. Firebase Auth already handles session management for you, so there's no need to cache the password yourself.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Logging in with email/password | Thuta Learning