0
0
FirebaseHow-ToBeginner · 4 min read

How to Use Firebase Authentication: Simple Guide for Beginners

To use Firebase Authentication, first initialize Firebase in your app, then use its methods like createUserWithEmailAndPassword to sign up users and signInWithEmailAndPassword to sign in. These methods handle user credentials securely and provide user info after successful login.
📐

Syntax

Firebase Authentication uses simple methods to manage users. The main methods are:

  • createUserWithEmailAndPassword(auth, email, password): Registers a new user.
  • signInWithEmailAndPassword(auth, email, password): Logs in an existing user.
  • signOut(auth): Logs out the current user.

Here, auth is the Firebase Authentication instance, email and password are strings from user input.

javascript
import { initializeApp } from 'firebase/app';
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut } from 'firebase/auth';

const firebaseConfig = {
  apiKey: 'YOUR_API_KEY',
  authDomain: 'YOUR_AUTH_DOMAIN',
  projectId: 'YOUR_PROJECT_ID',
  // other config
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);

// Sign up syntax
createUserWithEmailAndPassword(auth, email, password)
  .then(userCredential => {
    const user = userCredential.user;
    // user signed up
  })
  .catch(error => {
    // handle error
  });

// Sign in syntax
signInWithEmailAndPassword(auth, email, password)
  .then(userCredential => {
    const user = userCredential.user;
    // user signed in
  })
  .catch(error => {
    // handle error
  });

// Sign out syntax
signOut(auth)
  .then(() => {
    // user signed out
  })
  .catch(error => {
    // handle error
  });
💻

Example

This example shows how to sign up a user, then sign in, and finally sign out using Firebase Authentication in a simple JavaScript app.

javascript
import { initializeApp } from 'firebase/app';
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut } from 'firebase/auth';

const firebaseConfig = {
  apiKey: 'AIzaSyD-example-key',
  authDomain: 'your-app.firebaseapp.com',
  projectId: 'your-app',
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);

async function demoAuth() {
  try {
    // Sign up new user
    const userCredential = await createUserWithEmailAndPassword(auth, 'user@example.com', 'password123');
    console.log('User signed up:', userCredential.user.email);

    // Sign in the same user
    const signInCredential = await signInWithEmailAndPassword(auth, 'user@example.com', 'password123');
    console.log('User signed in:', signInCredential.user.email);

    // Sign out user
    await signOut(auth);
    console.log('User signed out');
  } catch (error) {
    console.error('Error:', error.message);
  }
}

demoAuth();
Output
User signed up: user@example.com User signed in: user@example.com User signed out
⚠️

Common Pitfalls

Common mistakes when using Firebase Authentication include:

  • Not initializing Firebase properly before calling auth methods.
  • Using weak or invalid passwords that Firebase rejects.
  • Not handling errors from promises, causing silent failures.
  • Trying to sign up a user that already exists without catching the error.
  • Forgetting to secure API keys and config in production.

Always check error messages and handle them gracefully.

javascript
/* Wrong: Not handling errors */
createUserWithEmailAndPassword(auth, 'user@example.com', '123')
  .then(userCredential => {
    console.log('Signed up:', userCredential.user.email);
  });

/* Right: Handle errors properly */
createUserWithEmailAndPassword(auth, 'user@example.com', '123')
  .then(userCredential => {
    console.log('Signed up:', userCredential.user.email);
  })
  .catch(error => {
    console.error('Signup failed:', error.message);
  });
📊

Quick Reference

Remember these key points when using Firebase Authentication:

  • Initialize Firebase app before using auth.
  • Use createUserWithEmailAndPassword to register users.
  • Use signInWithEmailAndPassword to log in users.
  • Always handle errors from auth methods.
  • Use signOut to log out users.

Key Takeaways

Initialize Firebase app and get auth instance before using authentication methods.
Use createUserWithEmailAndPassword and signInWithEmailAndPassword for user registration and login.
Always handle errors from Firebase auth methods to avoid silent failures.
Sign out users with signOut(auth) when needed.
Keep your Firebase config secure and never expose sensitive keys publicly.