-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
228 lines (203 loc) · 5.53 KB
/
app.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import connectDB from "./database/connect.js";
import User from "./database/models/User.js";
import Album from "./database/models/Album.js";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
import cookieParser from "cookie-parser";
import multer from "multer";
import fs from "fs";
dotenv.config();
const app = express();
const corsOptions = {
origin: "http://localhost:3000",
credentials: true,
};
app.use(express.json());
app.use("/uploads", express.static("uploads"));
app.use(cors(corsOptions));
app.use(cookieParser());
const port = process.env.PORT || 8000;
var salt = bcrypt.genSaltSync(10);
const jwtSecret = process.env.ACCESS_TOKEN;
app.get("/test", (req, res) => {
res.json("test ok");
});
app.post("/login", async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({
email: email,
});
if (user) {
const checkPass = bcrypt.compareSync(password, user.password);
if (checkPass) {
jwt.sign(
{
userName: user.userName,
email: user.email,
id: user._id,
},
jwtSecret,
{},
(err, token) => {
if (err) throw err;
console.log("Successfully logged in");
res.cookie("token", token).json(user);
}
);
} else {
console.log("Password is incorrect");
res.status(422).json("password not matched");
}
} else {
console.log("User not found");
}
} catch (err) {
res.status(404);
}
});
app.post("/register", async (req, res) => {
const userData = req.body.data;
const { firstName, lastName, userName, email, password } = userData;
console.log(userData);
try {
const newUser = await User.create({
firstName: firstName,
lastName: lastName,
userName: userName,
email: email,
password: bcrypt.hashSync(password, salt),
type: "User",
});
res.json(newUser);
} catch (e) {
res.status(422).json({ message: "Error creating user" });
}
});
app.post("/create-admin", async (req, res) => {
const { name, email, password } = req.body;
try {
const newUser = await User.create({
name: name,
email: email,
password: bcrypt.hashSync(password, salt),
type: "Admin",
});
console.log("Admin created", newUser);
res.json(newUser);
} catch (e) {
res.status(422).json({ message: "Error creating user" });
}
});
app.get("/profile", async (req, res) => {
const cookies = req.cookies;
const token = cookies.token;
if (token) {
jwt.verify(token, jwtSecret, {}, async (err, userData) => {
if (err) throw err;
const { firstName, lastName, userName, email, id, imageURL } =
await User.findById(userData.id);
res.json({ firstName, lastName, userName, email, id, imageURL });
});
}
});
app.get("/profile/update", async (req, res) => {
const cookies = req.cookies;
const token = cookies.token;
if (token) {
jwt.verify(token, jwtSecret, {}, async (err, userData) => {
if (err) throw err;
const updatedUserData = req.body;
//Logic of Update User Data
});
}
});
app.get("/logout", (req, res) => {
console.log("Successfully logged out");
res.cookie("token", "").json(true);
});
const imagesMiddleware = multer({ dest: "uploads/" });
app.post(
"/images/upload",
imagesMiddleware.array("photos", 100),
(req, res) => {
const uploadedFiles = [];
for (let i = 0; i < req.files.length; i++) {
const { path, originalname } = req.files[i];
const parts = originalname.split(".");
const ext = parts[parts.length - 1];
const newPath = path + "." + ext;
fs.renameSync(path, newPath);
uploadedFiles.push(newPath.replace("uploads\\", ""));
}
res.json(uploadedFiles);
}
);
app.post("/album/create", (req, res) => {
const { token } = req.cookies;
const data = req.body.data;
const images = req.body.images;
const story = req.body.story;
data.images = images;
data.story = story;
if (token) {
jwt.verify(token, jwtSecret, {}, async (err, userData) => {
if (err) throw err;
const albumDoc = await Album.create({
userId: userData.id,
createdBy: userData.userName,
...data,
});
res.json(albumDoc);
});
}
});
app.get("/albums/list/get", (req, res) => {
const { token } = req.cookies;
if (token) {
jwt.verify(token, jwtSecret, {}, async (err, userData) => {
if (err) throw err;
const albumsList = await Album.find({ userId: userData.id });
res.json(albumsList);
});
}
});
app.get("/album/get/:id", (req, res) => {
const { id } = req.params;
try {
const { token } = req.cookies;
if (token) {
jwt.verify(token, jwtSecret, {}, async (err, userData) => {
if (err) throw err;
const albumDoc = await Album.findOne({ _id: id });
res.json(albumDoc);
});
}
} catch (e) {
res.status(404);
}
});
app.get("/albums/all/get", async (req, res) => {
try {
const allAlbums = await Album.find();
console.log(allAlbums);
res.json(allAlbums);
} catch (err) {
res.status(404);
}
});
app.get("/image2prompt/get", (req, res) => {});
const startServer = async () => {
try {
connectDB(process.env.MONGODB_URL);
} catch (error) {
console.error("Error connecting to MongoDb");
}
app.listen(port, () => {
console.log(`Server listening on port: http://localhost:${port}/`);
});
};
startServer();