How user registration and login works?
✅1.User Registration: How Data is Saved in DB
🔹 Frontend (e.g., React Form)
The user fills a registration form:
{
name: "Avinash",
email: "avi@example.com",
password: "MySecurePassword123"
}
🔹 Backend (Node.js + Express example)
-
Receive form data in an API endpoint:
app.post("/register", async (req, res) => {
const { name, email, password } = req.body;
...
});
-
Check if user already exists:
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: "User already exists" });
}
-
Hash the password using
bcrypt:
const bcrypt = require("bcrypt");
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds);
-
Save the user in the database (MongoDB):
const newUser = new User({ name, email, password: hashedPassword });
await newUser.save();
res.status(201).json({ message: "User registered successfully" });
✅ 🔐 Result:
Only the hashed version of the password is saved in the database:
{
"_id": "12345",
"name": "Avinash",
"email": "avi@example.com",
"password": "$2b$10$eJx12345fK...encryptedHash"
}
✅ 2. User Login: How Identity is Verified
🔹 Frontend:
User submits login form:
{
email: "avi@example.com",
password: "MySecurePassword123"
}
🔹 Backend:
-
Find user by email:
const user = await User.findOne({ email });
if (!user) {
return res.status(401).json({ message: "Invalid credentials" });
}
-
Compare entered password with hashed one:
const isMatch = await bcrypt.compare(req.body.password, user.password);
if (!isMatch) {
return res.status(401).json({ message: "Invalid credentials" });
}
-
Create a session or JWT (JSON Web Token):
const jwt = require("jsonwebtoken");
const token = jwt.sign({ id: user._id }, "your_jwt_secret", { expiresIn: "1d" });
res.json({ message: "Login successful", token });
✅ 3. What Happens Behind the Scenes
| Step | Purpose |
|---|---|
| Hashing | Irreversibly encrypts the password, making it unreadable. Even if database is hacked, attacker can't get real passwords. |
| Salting | Adds random characters to the password before hashing, to prevent dictionary and rainbow table attacks. |
| JWT Token | Used to authenticate the user in future requests, proving identity without saving sessions on the server. |
| Token Verification | Every protected API checks token validity before responding to the user. |
✅ Example MongoDB User Record:
{
"_id": "64a123456789",
"name": "Avinash",
"email": "avi@example.com",
"password": "$2b$10$aGjDqrGrV2ZhRjeqxkYMEuX/9QKv.aa5kIvga4X0fJtVSkRz.kPu6"
}
🔐 Summary:
| Feature | Description |
|---|---|
| Hashing | Password is hashed using bcrypt, never stored in plain text. |
| Verification | On login, bcrypt compares hash of input password with stored one. |
| JWT or Sessions | Authenticated users receive a token to access protected resources. |
User Registration :
Through frontend like register page collect the data like the name/username, email, password
eg:
{
name: "Avinash",
email: "avi@example.com",
password: "MySecurePassword123"
}
where controller already write what to do when user send these api request
for this we handle from this
ex:
🌍 Explore Our Projects
| Project | Link |
|---|---|
| 🎬 Telegram Bot | https://tpi.li/nXPuWjXk |
| 🍿 Movie Website | https://cine-link-hub.vercel.app/ |
| 📝 Blogging Platform | https://studio--blogger-showcase.us-central1.hosted.app/ |
| 💍 ShaadiCraft Page | ShaadiCraft |
Comments
Post a Comment