Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions server/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose=require('mongoose');
require('dotenv').config();

const connectDB=async()=>{
try{
await mongoose.connect(process.env.MONGO_URI
);
console.log("MongoDB connected");
}catch(err){
console.error("MongoDB connection failed:",err);
process.exit(1);
}
};

module.exports=connectDB;
59 changes: 32 additions & 27 deletions server/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,67 @@
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const User = require("../models/User");

const users = [];
const tokenBlacklist = [];

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

exports.registerUser = async (req, res) => {
console.log("Register body:", req.body);
const { email, password } = req.body;
if (users.find(user => user.email === email)) {
return res.status(400).json({ message: "User already exists" });
try {
console.log("Register body:", req.body);
const { email, password } = req.body;

if (!email || !password) {
return res.status(400).json({ message: "Email and password are required" });
}
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: "User already exists" });
}

const hash = await bcrypt.hash(password, 10);

const newUser = new User({ email, password: hash });
await newUser.save();

return res.status(201).json({ message: "User registered successfully" });
} catch (err) {
console.error("Register error:", err);
return res.status(500).json({ message: "Server error" });
}
const hash = await bcrypt.hash(password, 10);
users.push({ email, password: hash });
res.json({ message: "User registered" });
}
};

exports.loginUser = async (req, res) => {
try {
console.log("Login body:", req.body);

const { email, password } = req.body;

// Validate input
if (!email || !password) {
return res.status(400).json({ message: "Email and password are required" });
}

// Find user
const user = users.find(u => u.email === email);
const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ message: "Invalid credentials" });
}

// Compare password
const match = await bcrypt.compare(password, user.password);
if (!match) {
return res.status(400).json({ message: "Invalid credentials" });
}
const token = jwt.sign({ email: user.email }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN });

// Generate token
const token = jwt.sign({ email }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN });

// Send response
return res.status(200).json({ token });
} catch (err) {
console.error("Login error:", err);
return res.status(500).json({ message: "Server error" });
}
};

exports.logoutUser = (req, res) => {
const { token } = req.body;
if (!token) {
return res.status(400).json({ message: "Token required for logout" });
}
tokenBlacklist.push(token);
res.json({ message: "Logged out" });
}

exports.getUsers =() => users;

exports.getBlacklist =() => tokenBlacklist;
res.json({ message: "Logged out successfully" });
};
exports.getBlacklist = () => tokenBlacklist;
4 changes: 4 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ const express = require("express");
const http = require("http");
const { Server } = require("socket.io");
const cors = require("cors");
const connectDB = require("./config/db");
require('dotenv').config();

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

// Middleware
app.use(express.json());
Expand Down
13 changes: 13 additions & 0 deletions server/models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mongoose=require('mongoose');
const userSchema=new mongoose.Schema({
email:{
type:String,
required:true,
unique:true,
},
password:{
type:String,
required:true,
},
});
module.exports=mongoose.model("User",userSchema);
Loading
Loading