Skip to content

Commit 159a496

Browse files
authored
Merge pull request #36 from Shreyanshi210205/shreyanshi_db
feat: implement MongoDB integration to user login and register.
2 parents 6225676 + 09a8b6b commit 159a496

File tree

6 files changed

+273
-27
lines changed

6 files changed

+273
-27
lines changed

server/config/db.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const mongoose=require('mongoose');
2+
require('dotenv').config();
3+
4+
const connectDB=async()=>{
5+
try{
6+
await mongoose.connect(process.env.MONGO_URI
7+
);
8+
console.log("MongoDB connected");
9+
}catch(err){
10+
console.error("MongoDB connection failed:",err);
11+
process.exit(1);
12+
}
13+
};
14+
15+
module.exports=connectDB;
Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,67 @@
11
const bcrypt = require("bcrypt");
22
const jwt = require("jsonwebtoken");
3+
const User = require("../models/User");
34

4-
const users = [];
55
const tokenBlacklist = [];
66

77
const JWT_SECRET = process.env.JWT_SECRET || "your_jwt_secret";
88
const JWT_EXPIRES_IN = "1h";
99

1010
exports.registerUser = async (req, res) => {
11-
console.log("Register body:", req.body);
12-
const { email, password } = req.body;
13-
if (users.find(user => user.email === email)) {
14-
return res.status(400).json({ message: "User already exists" });
11+
try {
12+
console.log("Register body:", req.body);
13+
const { email, password } = req.body;
14+
15+
if (!email || !password) {
16+
return res.status(400).json({ message: "Email and password are required" });
17+
}
18+
const existingUser = await User.findOne({ email });
19+
if (existingUser) {
20+
return res.status(400).json({ message: "User already exists" });
21+
}
22+
23+
const hash = await bcrypt.hash(password, 10);
24+
25+
const newUser = new User({ email, password: hash });
26+
await newUser.save();
27+
28+
return res.status(201).json({ message: "User registered successfully" });
29+
} catch (err) {
30+
console.error("Register error:", err);
31+
return res.status(500).json({ message: "Server error" });
1532
}
16-
const hash = await bcrypt.hash(password, 10);
17-
users.push({ email, password: hash });
18-
res.json({ message: "User registered" });
19-
}
33+
};
34+
2035
exports.loginUser = async (req, res) => {
2136
try {
2237
console.log("Login body:", req.body);
23-
2438
const { email, password } = req.body;
2539

26-
// Validate input
2740
if (!email || !password) {
2841
return res.status(400).json({ message: "Email and password are required" });
2942
}
30-
31-
// Find user
32-
const user = users.find(u => u.email === email);
43+
const user = await User.findOne({ email });
3344
if (!user) {
3445
return res.status(400).json({ message: "Invalid credentials" });
3546
}
36-
37-
// Compare password
3847
const match = await bcrypt.compare(password, user.password);
3948
if (!match) {
4049
return res.status(400).json({ message: "Invalid credentials" });
4150
}
51+
const token = jwt.sign({ email: user.email }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN });
4252

43-
// Generate token
44-
const token = jwt.sign({ email }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN });
45-
46-
// Send response
4753
return res.status(200).json({ token });
4854
} catch (err) {
4955
console.error("Login error:", err);
5056
return res.status(500).json({ message: "Server error" });
5157
}
5258
};
53-
5459
exports.logoutUser = (req, res) => {
5560
const { token } = req.body;
61+
if (!token) {
62+
return res.status(400).json({ message: "Token required for logout" });
63+
}
5664
tokenBlacklist.push(token);
57-
res.json({ message: "Logged out" });
58-
}
59-
60-
exports.getUsers =() => users;
61-
62-
exports.getBlacklist =() => tokenBlacklist;
65+
res.json({ message: "Logged out successfully" });
66+
};
67+
exports.getBlacklist = () => tokenBlacklist;

server/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ const express = require("express");
22
const http = require("http");
33
const { Server } = require("socket.io");
44
const cors = require("cors");
5+
const connectDB = require("./config/db");
6+
require('dotenv').config();
57

68
const app = express();
79
const PORT = process.env.PORT || 3000;
10+
const URL=process.env.MONGO_URI||"mongodb://localhost:27017/collab-canvas";
11+
connectDB(URL);
812

913
// Middleware
1014
app.use(express.json());

server/models/User.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const mongoose=require('mongoose');
2+
const userSchema=new mongoose.Schema({
3+
email:{
4+
type:String,
5+
required:true,
6+
unique:true,
7+
},
8+
password:{
9+
type:String,
10+
required:true,
11+
},
12+
});
13+
module.exports=mongoose.model("User",userSchema);

0 commit comments

Comments
 (0)