In this tutorial, we'll guide you through setting up authentication for your web application using Google Firebase. We’ll build a simple message board application where users can sign up and sign in securely with either email and password or their Google account.
- Introduction
- Prerequisites
- Setting Up the Development Environment
- Creating a Firebase Project
- Setting Up Firebase Authentication
- Implementing Authentication in Your App
- Handling User Authentication States
- Testing the Application
- Next Steps
Firebase is a platform by Google that helps you develop, build, and manage mobile and web apps easily by providing tools like authentication, databases, cloud functions, and web hosting. In this tutorial, we'll focus on Firebase Authentication.
Before starting, make sure you have the following installed on your machine:
- Node.js (latest LTS version)
-
Install Vite:
npm install -g create-vite
-
Create a New Project:
npx create-vite@latest firebase-tutorial --template vanilla
-
Navigate to the Project Directory:
cd firebase-tutorial -
Install Dependencies:
npm install
-
Start the Development Server:
npm run dev
- Go to the Firebase Console.
- Click on "Add project" and follow the steps to create a new project. (e.g.,
message-app) - Disable Google Analytics (optional) and click "Create Project".
-
In your Firebase project console, click on the web icon (
</>). -
Register your app (e.g.,
message-app) and click "Register App". -
Copy the Firebase SDK configuration code and paste it into your
main.jsfile. -
Install Firebase in your project:
npm install firebase
-
In your
main.jsfile, initialize Firebase with the provided configuration:import { initializeApp } from "firebase/app"; import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signInWithPopup, GoogleAuthProvider, onAuthStateChanged, signOut } from "firebase/auth"; const firebaseConfig = { apiKey: "your-api-key", authDomain: "your-auth-domain", projectId: "your-project-id", storageBucket: "your-storage-bucket", messagingSenderId: "your-messaging-sender-id", appId: "your-app-id" }; const app = initializeApp(firebaseConfig); const auth = getAuth(app);
-
Sign Up with Email and Password:
const emailSignUpForm = document.getElementById('emailSignUpForm'); const passwordSignUpForm = document.getElementById('passwordSignUpForm'); const signUpButton = document.getElementById('signUpButton'); signUpButton.addEventListener('click', () => { const email = emailSignUpForm.value; const password = passwordSignUpForm.value; createUserWithEmailAndPassword(auth, email, password) .then((userCredential) => { console.log('User signed up:', userCredential.user); }) .catch((error) => { console.error('Error signing up:', error.message); }); });
-
Sign In with Email and Password:
const emailSignInForm = document.getElementById('emailSignInForm'); const passwordSignInForm = document.getElementById('passwordSignInForm'); const signInButton = document.getElementById('signInButton'); signInButton.addEventListener('click', () => { const email = emailSignInForm.value; const password = passwordSignInForm.value; signInWithEmailAndPassword(auth, email, password) .then((userCredential) => { console.log('User signed in:', userCredential.user); }) .catch((error) => { console.error('Error signing in:', error.message); }); });
-
Sign In with Google:
const googleSignInButton = document.getElementById('googleSignInButton'); const provider = new GoogleAuthProvider(); googleSignInButton.addEventListener('click', () => { signInWithPopup(auth, provider) .then((result) => { console.log('User signed in with Google:', result.user); }) .catch((error) => { console.error('Error signing in with Google:', error.message); }); });
-
Monitor Auth State Changes:
const loggedOutView = document.getElementById('loggedOutView'); const loggedInView = document.getElementById('loggedInView'); const userEmail = document.getElementById('userEmail'); onAuthStateChanged(auth, (user) => { if (user) { loggedInView.style.display = 'block'; loggedOutView.style.display = 'none'; userEmail.textContent = `You are logged in as ${user.email}`; } else { loggedInView.style.display = 'none'; loggedOutView.style.display = 'block'; } });
-
Sign Out:
const logoutButton = document.getElementById('logoutButton'); logoutButton.addEventListener('click', () => { signOut(auth) .then(() => { console.log('User signed out'); }) .catch((error) => { console.error('Error signing out:', error.message); }); });
-
Run the Development Server:
npm run dev
-
Open Your App in the Browser: Navigate to
http://localhost:5174(or the port specified by Vite) and test the sign-up, sign-in, and sign-out functionalities.
In the next tutorial, we will connect our application to a Firestore database to enable creating and viewing posts. Stay tuned!