TGViewer
Web Development Web Development @webdevcoursefree ยท 79.6K subscribers
Post #3599 5.09K
๐Ÿ”‘ Authentication (Login Signup with JWT

Now you learn how real apps handle users and security.

๐Ÿ‘‰ Every real app needs:
โ€ข Signup (register)
โ€ข Login (authenticate)
โ€ข Protected routes

๐Ÿง  What is Authentication
Authentication = verifying who the user is
Example:
โ€ข Login with email + password
โ€ข System checks credentials
โ€ข Grants access

๐Ÿ”‘ What is JWT
JWT = JSON Web Token
๐Ÿ‘‰ A secure token sent after login
๐Ÿ‘‰ Used to identify user in future requests
Simple flow:
1๏ธโƒฃ User logs in
2๏ธโƒฃ Server creates token
3๏ธโƒฃ Token sent to frontend
4๏ธโƒฃ Frontend sends token in every request

๐Ÿ“ฆ Install Required Packages
npm install jsonwebtoken bcryptjs
โ€ข jsonwebtoken โ†’ create token
โ€ข bcryptjs โ†’ hash passwords

๐Ÿงฉ Step 1 โ€” User Schema
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
email: String,
password: String
});
const User = mongoose.model("User", userSchema);

๐Ÿ” Step 2 โ€” Signup API
const bcrypt = require("bcryptjs");
app.post("/signup", async (req, res) => {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const user = new User({
email: req.body.email,
password: hashedPassword
});
await user.save();
res.json({ message: "User registered" });
});
โœ” Password is stored securely
โœ” Never store plain text password

๐Ÿ”“ Step 3 โ€” Login API
const jwt = require("jsonwebtoken");
app.post("/login", async (req, res) => {
const user = await User.findOne({ email: req.body.email });
if (!user) {
return res.status(400).json({ message: "User not found" });
}
const isMatch = await bcrypt.compare(req.body.password, user.password);
if (!isMatch) {
return res.status(400).json({ message: "Invalid credentials" });
}
const token = jwt.sign(
{ userId: user._id },
"secretkey",
{ expiresIn: "1h" }
);
res.json({ token });
});
โœ” Validates user
โœ” Generates token

๐Ÿ›ก๏ธ Step 4 โ€” Protect Routes (Middleware)
const authMiddleware = (req, res, next) => {
const token = req.headers.authorization;
if (!token) {
return res.status(401).json({ message: "Access denied" });
}
try {
const verified = jwt.verify(token, "secretkey");
req.user = verified;
next();
} catch {
res.status(400).json({ message: "Invalid token" });
}
};

๐Ÿ”’ Step 5 โ€” Use Protected Route
app.get("/profile", authMiddleware, (req, res) => {
res.json({ message: "Welcome user", user: req.user });
});
โœ” Only logged-in users can access

๐Ÿ”„ Full Authentication Flow
Signup โ†’ Store user
Login โ†’ Verify user โ†’ Generate token
Frontend stores token
Frontend sends token in requests
Backend verifies token

โš ๏ธ Common Beginner Mistakes
โ€ข Storing plain passwords โŒ
โ€ข Not hashing passwords โŒ
โ€ข Exposing secret key โŒ
โ€ข Not verifying token โŒ

๐Ÿงช Mini Practice Task
Build authentication system:
โ€ข POST /signup
โ€ข POST /login
โ€ข GET /dashboard (protected route)
โ€ข Use JWT middleware

Double Tap โ™ฅ๏ธ For More
  • โค 27
  • ๐Ÿ‘ 1
More from @webdevcoursefree
  1. Oct 14, 2026๐ŸŒ Web Development Roadmap โ€” Part 17 HTML Images โ€” <img> Tag Images make webpages more visโ€ฆ
  2. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  3. Oct 12, 2026For example: Go to Profile โ†’ <a> Submit Form โ†’ <button> Open Article โ†’ <a> Delete Accountโ€ฆ
  4. Oct 12, 2026๐ŸŒ Web Development Roadmap โ€” Part 16 HTML Links โ€” Tag Links are one of the most importantโ€ฆ
  5. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  6. Oct 9, 2026Post #4079
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’