-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (80 loc) · 2.41 KB
/
server.js
File metadata and controls
97 lines (80 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import jwt from "jsonwebtoken";
import bcrypt from "bcryptjs";
import { PrismaClient } from "@prisma/client";
import downloadRoutes from "./routes/download.routes.js";
// Load environment variables
dotenv.config();
// Initialize Prisma client
const prisma = new PrismaClient();
// Create Express app
const app = express();
app.use(cors());
app.use(express.json());
// Register Route
app.post("/signup", async (req, res) => {
try {
const {
fullName,
email,
rollNumber,
dateOfBirth,
school,
program,
specialization,
password,
confirmPassword
} = req.body;
if (!fullName || !email || !password || !confirmPassword) {
return res.status(400).json({ message: "All required fields must be filled." });
}
if (password !== confirmPassword) {
return res.status(400).json({ message: "Passwords do not match." });
}
const existingUser = await prisma.user.findUnique({ where: { email } });
if (existingUser) {
return res.status(409).json({ message: "User already exists with this email." });
}
const hashedPassword = await bcrypt.hash(password, 10);
await prisma.user.create({
data: {
fullName,
email,
rollNumber,
dateOfBirth,
school,
program,
specialization,
password: hashedPassword
}
});
res.status(201).json({ message: "User registered successfully." });
} catch (error) {
console.error("Signup error:", error);
res.status(500).json({ message: "Server error during signup." });
}
});
// Login Route
app.post("/login", async (req, res) => {
try {
const { email, password } = req.body;
const user = await prisma.user.findUnique({ where: { email } });
if (!user || !(await bcrypt.compare(password, user.password))) {
return res.status(401).json({ message: "Invalid email or password." });
}
const token = jwt.sign({ email: user.email }, process.env.JWT_SECRET, {
expiresIn: "1d"
});
res.json({ token });
} catch (error) {
console.error("Login error:", error);
res.status(500).json({ message: "Server error during login." });
}
});
// Routes
app.use("/api/download", downloadRoutes);
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));