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.