Skip to content

Commit bee00d6

Browse files
committed
added auth backend
1 parent a031b0f commit bee00d6

File tree

4 files changed

+255
-2
lines changed

4 files changed

+255
-2
lines changed

server/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PORT=5000
2+
MONGO_URI=your_mongodb_connection_string
3+
CORS_ORIGIN=http://localhost:3000

server/index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,69 @@
11
const express = require("express");
2+
require("dotenv").config();
3+
const bcrypt = require("bcrypt");
4+
const jwt = require("jsonwebtoken");
5+
const cors = require("cors");
26
const app = express();
7+
app.use(express.json());
8+
app.use(cors());
39
const PORT = process.env.PORT || 3000;
410

11+
const users = [];
12+
const tokenBlacklist=[];
13+
14+
const JWT_SECRET = process.env.JWT_SECRET || "your_jwt_secret";
15+
const JWT_EXPIRES_IN = "1h";
16+
17+
app.post("/signup",async (req, res) => {
18+
const { email, password } = req.body;
19+
if (users.find(user => user.email === email)) {
20+
return res.status(400).json({ message: "User already exists" });}
21+
const hash = await bcrypt.hash(password, 10);
22+
users.push({ email, password: hash });
23+
res.json({ message: "User registered" });
24+
});
25+
26+
app.post("/login", async (req, res) => {
27+
const { email, password } = req.body;
28+
const user = users.find((u) => u.email === email);
29+
if (!user) return res.status(400).json({ message: "Invalid credentials" });
30+
31+
const match = await bcrypt.compare(password, user.password);
32+
if (!match) return res.status(400).json({ message: "Invalid credentials" });
33+
34+
const token = jwt.sign({ email }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN });
35+
res.json({ token });
36+
});
37+
38+
function authMiddleware(req, res, next) {
39+
const auth = req.headers.authorization;
40+
if (!auth) return res.status(401).json({ message: "No token" });
41+
42+
const token = auth.split(" ")[1];
43+
if (tokenBlacklist.includes(token)) {
44+
return res.status(403).json({ message: "Logged out" });
45+
}
46+
47+
try {
48+
const payload = jwt.verify(token, JWT_SECRET);
49+
req.user = payload;
50+
next();
51+
} catch {
52+
res.status(403).json({ message: "Invalid token" });
53+
}
54+
}
55+
56+
57+
app.get("/me", authMiddleware, (req, res) => {
58+
res.json({ user: req.user });
59+
});
60+
61+
app.post("/logout", (req, res) => {
62+
const { token } = req.body;
63+
tokenBlacklist.push(token);
64+
res.json({ message: "Logged out" });
65+
});
66+
567
app.get("/", (req, res) => {
668
res.send("Collab Canvas server is running!");
769
});

server/package-lock.json

Lines changed: 185 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"test": "echo \"Error: no test specified\" && exit 1"
1111
},
1212
"dependencies": {
13-
"express": "^5.1.0"
13+
"bcrypt": "^6.0.0",
14+
"cors": "^2.8.5",
15+
"dotenv": "^17.2.3",
16+
"express": "^5.1.0",
17+
"jsonwebtoken": "^9.0.2"
1418
}
1519
}

0 commit comments

Comments
 (0)