Getting Started with Firebase Email/Password Authentication (original) (raw)

Last Updated : 23 Jul, 2025

**Email/**password authentication is a widely used method for users to sign in to applications securely. It offers a **familiar and convenient way for users to access their **accounts.

Firebase Authentication simplifies the implementation of this process by handling backend tasks securely, such as **storing **passwords, **managing user **sessions, and integrating with popular identity providers like **Google and **Facebook.

In this article, we will learn about how to set up and implement email/password authentication in a web application using **Firebase Authentication.

Understanding Email/Password Authentication

Setting up Firebase Authentication

Before understanding the implementation we have to set up a Firebase Authentication in our Firebase project by following the below steps:

With Firebase set up, we are ready to implement email/password authentication in our application.

Implementing Email/Password Authentication in Web Apps

Let's understand the steps to implement email/password authentication in a web app using Firebase SDK. We'll start with a simple **HTML page and add JavaScript for Firebase integration.

1. HTML Setup

Let's Create a simple HTML file that allows users to sign in or sign up using their email and password. Use Firebase Authentication to handle the authentication process.

Firebase Authentication

Email/Password Authentication

Sign In Sign Up
<script src="https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js"></script>  
<script src="https://www.gstatic.com/firebasejs/9.0.2/firebase-auth.js"></script>  
<script src="firebase-config.js"></script>  
<script src="app.js"></script>  

**Output:

simpleHTML

**Explanation

2. Firebase Configuration

Let's Create a firebase-config.js file with our Firebase project configuration:

const firebaseConfig = {
apiKey: "AIzaSyDmaZAcK7xwQTAsQJxaGnG56oB8RIJDMnc",
authDomain: "samplep-d6b68.firebaseapp.com",,
projectId: "samplep-d6b68",
storageBucket:"samplep-d6b68.appspot.com",
messagingSenderId:"398502093281",
appId:"1:398502093281:web:5d685b0733d4d6d66472a0",
measurementId: "G-9E6K9YD8D1"
};

const app = firebase.initializeApp(firebaseConfig);

**Explanation:

3. Authentication Logic (app.js)

This below code handles user authentication using Firebase email and password authentication. The signIn function allows existing users to sign in while the signUp function creates a new user account.

Both functions capture the email and password from input fields, then use Firebase authentication methods to sign in or sign up the user. Any errors during the process are logged to the console.

const auth = firebase.auth();

function signIn() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;

auth.signInWithEmailAndPassword(email, password)  
    .then((userCredential) => {  
        // Signed in successfully  
        const user = userCredential.user;  
        console.log('Signed in as:', user.email);  
    })  
    .catch((error) => {  
        console.error('Sign in error:', error.message);  
    });  

}

function signUp() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;

auth.createUserWithEmailAndPassword(email, password)  
    .then((userCredential) => {  
        // Signed up successfully  
        const user = userCredential.user;  
        console.log('Signed up as:', user.email);  
    })  
    .catch((error) => {  
        console.error('Sign up error:', error.message);  
    });  

}

**Explanation

These functions use Firebase Authentication to handle sign-in and sign-up operations using email and password.

  1. signIn() : Retrieves the email and password values from the input fields, then uses auth. signInWithEmailAndPassword(email, password) to authenticate the user. If successful it logs the users email to the console. If there's an error it logs the error message.
  2. signUp() : Similar to signIn(), but uses auth. createUserWithEmailAndPassword(email, password) to create a new user account if the provided email doesn't already exist. If successful it logs the users email to the console. If there's an error it logs the error message

Testing Email/Password Authentication

Now, let's test the authentication flow in our web app:

Output:

emailpassword

Conclusion

Overall, Email/password authentication is a fundamental aspect of user authentication in web applications. Firebase Authentication provides a simple and secure way to implement this functionality, allowing users to sign in using their email address and password. By following the steps outlined in this guide, you can easily integrate email/password authentication into your web application using Firebase Authentication