-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (127 loc) · 4.62 KB
/
index.js
File metadata and controls
145 lines (127 loc) · 4.62 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import express from 'express';
import cors from 'cors';
import db from './db/database.mjs';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import multer from 'multer';
const app = express();
const PORT = 3001;
const JWT_SECRET = 'rtsdeveloper';
const upload = multer({ limits: { fileSize: 50 * 1024 * 1024 } });
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
app.use(cors());
const verifyToken = (req, res, next) => {
const token = req.headers['authorization'];
if (!token) {
return res.status(401).send({ message: 'No token provided' });
}
jwt.verify(token.split(" ")[1], JWT_SECRET, (err, decoded) => {
if (err) {
return res.status(401).send({ message: 'Failed to authenticate token' });
}
req.userId = decoded.id;
next();
});
};
app.post("/api/register", async (req, res) => {
const { name, email, username, password, profile_photo } = req.body;
if (!name || !email || !username || !password) {
return res.status(400).send({ message: 'Bad request' });
}
try {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
await db.create({
name,
email,
username,
password: hashedPassword,
profile_photo
});
return res.status(201).send({ message: 'User created successfully', status: 201 });
} catch (error) {
console.error('Error creating user:', error);
return res.status(500).send({ message: 'Internal Server Error' });
}
});
app.post("/api/login", async (req, res) => {
const { email, password } = req.body;
if (!email || !password) {
return res.status(400).send({ message: 'Bad request' });
}
const user = await db.findOne({ email });
if (!user) {
return res.status(404).send({ message: 'User not found', status: 404 });
}
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(401).send({ message: 'Invalid credentials', status: 401 });
}
const token = jwt.sign({ id: user._id }, JWT_SECRET, { expiresIn: '1h' });
res.status(200).send({
status: 200,
message: 'Successfully logged in',
data: {
token
},
});
});
app.get('/api/profile', verifyToken, async (req, res) => {
try {
const user = await db.findById(req.userId);
if (!user) {
return res.status(404).send({ message: 'User not found' });
}
return res.status(200).send({
status: 200,
message: 'Successfully fetched current user data',
data: {
name: user.name,
email: user.email,
username: user.username,
profile_photo: user.profile_photo
},
});
} catch (error) {
return res.status(500).send({ message: 'Internal server error' });
}
});
app.patch('/api/update-profile', verifyToken, upload.single('file'), async (req, res) => {
try {
const { name, email, username, profile_photo } = req.body;
const user = await db.findById(req.userId);
if (!user) {
return res.status(404).send({ status: 404, message: 'User not found' });
}
await db.updateOne({ _id: req.userId }, { name, email, username, profile_photo });
return res.status(200).send({
status: 200,
message: 'Successfully updated current user data',
});
} catch (error) {
return res.status(500).send({ message: 'Internal server error' });
}
});
app.post('/api/forgot-password', async (req, res) => {
try {
const { email, oldPassword, newPassword } = req.body;
const user = await db.findOne({ email });
if (!user) {
return res.status(404).send({ status: 404, message: 'User not found' });
}
const isMatch = await bcrypt.compare(oldPassword, user.password);
if (!isMatch) {
return res.status(401).send({ status: 401, message: 'Invalid old password' });
}
const salt = await bcrypt.genSalt(10);
const hashedNewPassword = await bcrypt.hash(newPassword, salt);
await db.updateOne({ _id: user._id }, { password: hashedNewPassword });
return res.status(200).send({ status: 200, message: 'Password reset successfully' });
} catch (error) {
return res.status(500).send({ message: 'Internal server error' });
}
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});