Multi Factor authentication using MERN
Last Updated :
24 Apr, 2025
This article will guide you through creating a Multi-Factor Authentication (MFA) project using the MERN. This project aims to enhance security by implementing a multi-step authentication process. Users will be required to provide multiple forms of identification for access, adding an extra layer of protection.
Output Preview:
Output PreviewPrerequisites / Technologies Used:
Approach:
The project will include:
- User login with MFA
- Generation and validation of one-time passwords (OTPs)
- Integration of OTP-based multi-factor authentication into the login process
Project Structure:
Folder structurepackage.json:
Frontend
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Backend
package.json:
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"mongoose": "^7.6.5",
"nodemailer": "^6.9.7",
"randomatic": "^3.1.1",
}
Steps to Create MERN Application:
Step 1: Create a new project directory:
mkdir mfa-mern
cd mfa-mern
Step 2: Initialize the project and set up the client and server directories:
npm init -y
mkdir client server
Step 3: Set Up the Backend (Node.js, Express.js, MongoDB)
cd server
npm init -y
Step 4: Install necessary dependencies:
npm install express mongoose nodemailer cors body-parser randomatic
Step 5: Set Up the Frontend (React)
cd ../client
npx create-react-app .
Step 6: Install necessary dependencies:
npm install react-router-dom
Step 7: Set Up MongoDB
Make sure you have MongoDB installed on your system. Start MongoDB and create a database named mfa-mern.
Implementation of Multi-Factor Authentication:
frontend:
JavaScript
// client/src/components/Login.js
import React, { useState } from 'react';
import axios from 'axios';
import './Login.css';
const Login = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [otp, setOtp] = useState('');
const [showOtpField, setShowOtpField] = useState(false);
const handleLogin = async () => {
try {
const response =
await axios.post(
'http://localhost:3001/auth/login',
{
email,
password
}
);
if (response.data.success) {
setShowOtpField(true);
alert('OTP sent to your email. Check your inbox.');
} else {
alert(response.data.message);
}
} catch (error) {
console.error('Error during login:', error.message);
alert('An error occurred during login');
}
};
const handleOtpVerification = async () => {
try {
const otpResponse =
await
axios.post(
'http://localhost:3001/auth/verify-otp',
{
otp
}
);
if (otpResponse.data.success) {
alert('OTP Verified. User logged in.');
// Redirect to your dashboard or perform any
// additional actions for successful login
} else {
alert('Invalid OTP. Please try again.');
}
} catch (error) {
console.error('Error during OTP verification:', error.message);
alert('An error occurred during OTP verification');
}
};
return (
<div className="login-container">
<input type="email"
placeholder="Email"
onChange={
(e) =>
setEmail(e.target.value)} />
<input type="password"
placeholder="Password"
onChange={
(e) =>
setPassword(e.target.value)} />
{showOtpField && (
<>
<input type="text"
placeholder="OTP"
onChange={
(e) =>
setOtp(e.target.value)} />
<button className="login-button"
onClick={handleOtpVerification}>
Verify OTP
</button>
</>
)}
<button className="login-button"
onClick={handleLogin}>
Login
</button>
</div>
);
};
export default Login;
JavaScript
// client/src/App.js
import React from 'react';
import Login from './components/Login';
function App() {
return (
<div className="App">
<div style={centerStyle}>
<h1>
MFA using
MERN Stack
</h1>
</div>
<Login />
</div>
);
}
const centerStyle = {
textAlign: 'center',
};
export default App;
Backend:
JavaScript
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const nodemailer = require('nodemailer');
const randomize = require('randomatic');
const app = express();
const PORT = 3001;
app.use(bodyParser.json());
app.use(cors());
// Connect to MongoDB with connection pooling
mongoose.connect('mongodb://127.0.0.1:27017/mfa-mern', {
useNewUrlParser: true,
useUnifiedTopology: true,
// poolSize: 10,
});
const User = mongoose.model('User', {
email: String,
password: String,
otp: String,
});
// Function to send OTP to the user's email
async function sendOtpEmail(email, otp) {
try {
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
// replace with your email and password
user: 'your-email',
pass: 'your-password',
},
});
const mailOptions = {
from: '[email protected]',
to: email,
subject: 'OTP Verification',
text: `Your OTP is: ${otp}`,
};
const info =
await transporter.sendMail(mailOptions);
console.log('Email sent: ' + info.response);
} catch (error) {
console.error('Error sending email:', error);
}
}
app.post('/auth/login', async (req, res) => {
const { email, password } = req.body;
console.log(req.body)
try {
const user = await User.findOne({ email, password });
console.log(user)
if (!user) {
return res.json(
{
success: false,
message: 'Invalid credentials'
}
);
}
const generatedOtp = randomize('0', 6);
user.otp = generatedOtp;
await user.save();
sendOtpEmail(email, generatedOtp);
return res.json({ success: true });
} catch (error) {
console.error('Error during login:', error.message);
return res.status(500).json(
{
success: false,
message: 'An error occurred during login'
}
);
}
});
app.post('/auth/verify-otp', async (req, res) => {
const { otp } = req.body;
try {
const user = await User.findOne({ otp });
if (!user) {
return res.json({ success: false, message: 'Invalid OTP' });
}
user.otp = '';
await user.save();
return res.json({ success: true });
} catch (error) {
console.error('Error during OTP verification:', error.message);
return res.status(500)
.json(
{
success: false,
message: 'An error occurred during OTP verification'
}
);
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Step to Run Application: Run the application using the following command from the root directory of the project.
Start the server:
cd server
node server.js
Start the client:
cd client
npm start
Output: Open your browser and go to http://localhost:3000 to view the application.
Output of the project
Similar Reads
35+ MERN Stack Projects with Source Code [2024]
The MERN stack, comprising MongoDB, Express JS, React, and Node JS, is a powerful combination that enables developers to build full-stack web applications using JavaScript. MongoDB serves as the database, Express.js handles server-side logic, React manages the client-side interface, and Node.js faci
6 min read
Stock Market Portfolio App using MERN Stack
The Stock Market Portfolio project is a web application that helps to efficiently manage and track stock market investments and portfolios. In this article, we will see a step-wise process of building a Stock Market Portfolio using the power of the MERN stack. Project Preview: Let us have a look at
5 min read
Hospital Management Application using MERN Stack
In the fast-paced world of healthcare, it's vital to manage hospital tasks effectively to ensure top-notch patient care. This article explores creating a strong Hospital Management App using the MERN stack â that's MongoDB, Express, React, and Node.js, breaking down the process for easier understand
15+ min read
Social Media Platform using MERN Stack
In web development, creating a "Social Media Website" will showcase and utilising the power of MERN stack â MongoDB, Express, React, and Node. This application will provide users the functionality to add a post, like the post and able to comment on it.Preview Image: Let us have a look at how the fin
7 min read
Bookstore Ecommerce App using MERN Stack
Bookstore E-commerce project is a great way to showcase your understanding of full-stack development. In this article, we'll walk through the step-by-step process of creating a Bookstore E-commerce using the MERN (MongoDB, Express.js, React, Node.js) stack. This project will showcase how to set up a
8 min read
Chat Website using MERN Stack
The "Chat Website" project is a dynamic web application that is used for real-time communication. The MERN stack, comprised of MongoDB, Express.js, React.js, and Node.js, is a powerful combination of technologies for developing robust and scalable web applications. In this article, we'll explore the
4 min read
Restaurant App using MERN Stack
Creating a Restaurant app will cover a lot of features of the MERN stack. In this tutorial, we'll guide you through the process of creating a restaurant application using the MERN stack. The application will allow users to browse through a list of restaurants, view their menus, and add items to a sh
11 min read
Real Estate Management using MERN
In this article, we will guide you through the process of building a Real Estate Management Application using the MERN stack. MERN stands for MongoDB, Express, React, and Node. MongoDB will serve as our database, Express will handle the backend, React will create the frontend, and Node.js will be th
9 min read
Fruit and Vegetable Market Shop using MERN
In this comprehensive guide, we'll walk through the step-by-step process of creating a Fruit and Vegetable Market Shop using the MERN (MongoDB, Express.js, React, Node.js) stack. This project will showcase how to set up a full-stack web application where users can view, filter, and purchase various
8 min read
Event Management Web App using MERN
In this guide, we'll walk through the step-by-step process of building a feature-rich Event Management Web App. We will make use of the MERN stack to build this project.Preview of final output: Let us have a look at how the final output will look like.Final Output of Event Management AppPrerequisite
9 min read