Back to Tutorials
Backend Projects
50 min read
Sahasransu Satpathy
4/15/2026
Backend Project: Login & Registration System
Build a full backend login and registration system using Node.js, Express, and MongoDB
Introduction
In this project, you'll build a login and registration system with Node.js, Express, and MongoDB. This includes user authentication, password hashing, and session management.
Step 1: Project Setup
Create a new project folder and initialize Node.js:
mkdir login-registration-system
cd login-registration-system
npm init -y
npm install express mongoose bcryptjs jsonwebtoken dotenv
Step 2: Setting Up Express Server
const express = require('express');
const app = express();
app.use(express.json());
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Step 3: Connecting to MongoDB
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB connected'))
.catch(err => console.error(err));
Step 4: Creating User Model
const { Schema, model } = require('mongoose');
const bcrypt = require('bcryptjs');
const userSchema = new Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
});
userSchema.pre('save', async function(next) {
if(!this.isModified('password')) return next();
this.password = await bcrypt.hash(this.password, 10);
next();
});
const User = model('User', userSchema);
module.exports = User;
Step 5: Registration Route
const express = require('express');
const router = express.Router();
const User = require('./models/User');
router.post('/register', async (req, res) => {
const { username, email, password } = req.body;
try {
const user = new User({ username, email, password });
await user.save();
res.status(201).json({ message: 'User registered successfully' });
} catch(err) {
res.status(400).json({ error: err.message });
}
});
module.exports = router;
Step 6: Login Route with JWT
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
router.post('/login', async (req, res) => {
const { email, password } = req.body;
const user = await User.findOne({ email });
if(!user) return res.status(400).json({ error: 'User not found' });
const isMatch = await bcrypt.compare(password, user.password);
if(!isMatch) return res.status(400).json({ error: 'Invalid credentials' });
const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: '1h' });
res.json({ message: 'Login successful', token });
});
Step 7: Protecting Routes
const authMiddleware = (req, res, next) => {
const token = req.header('Authorization')?.replace('Bearer ', '');
if(!token) return res.status(401).json({ error: 'Access denied' });
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
} catch(err) {
res.status(400).json({ error: 'Invalid token' });
}
};
Step 8: Testing the System
- Use Postman or Insomnia to test registration and login endpoints.
- Check token generation and protected routes.
Step 9: Best Practices
- Always hash passwords before storing.
- Use environment variables for secrets.
- Implement email verification for added security.
- Validate user input on server side.
Conclusion
By completing this project, you now have a secure backend login and registration system ready for integration with frontend applications.
SEO Suggestions:
- Main keywords: Node.js login system, Express registration tutorial, JWT authentication, backend authentication guide
- Meta description: Learn how to build a login and registration system using Node.js, Express, and MongoDB with JWT authentication. Step-by-step beginner-friendly guide.
- Catchy title suggestions: "Node.js Login & Registration System – Step-by-Step Guide", "Build Secure Authentication with Node.js, Express & MongoDB"
Previous Tutorial
Browse All TutorialsNext Tutorial
Browse All Tutorials