-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
157 lines (131 loc) · 3.63 KB
/
server.ts
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
import express from "express";
import cors from "cors";
import { db } from "./utils/db";
import { productRoute } from "./resources/product";
import { categoryRoute } from "./resources/category";
import { uploadImages } from "./middleware/multerMiddleware";
import { getAuth } from "firebase-admin/auth";
import { initializeApp, cert } from "firebase-admin/app";
import path from "path";
import sharp from "sharp";
const app = express();
// init firebase admin sdkk
const serviceAccount = require(path.join(__dirname, "firebase.json"));
initializeApp({
credential: cert(serviceAccount),
});
app.use(express.json());
app.use(
cors({
origin: ["http://localhost:3000", "http://localhost:3001"],
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
})
);
app.post("/auth/registration", async (req, res) => {
const { token } = req.body;
console.log({ token });
if (!token) {
return res.status(400).send("Missing token");
}
try {
// Verifying token
const decodedToken = await getAuth().verifyIdToken(token);
const { email, name, picture } = decodedToken;
console.log("Decoded Token:", decodedToken);
console.log({ email, name, picture });
let newUser = await db.user.findFirst({
where: {
email: email,
},
});
let hasUsername = false;
if (!newUser) {
newUser = await db.user.create({
data: {
email: email!,
username: name,
profilePic: picture!,
},
});
} else {
if (newUser.username) {
hasUsername = true;
} else {
newUser = await db.user.update({
where: {
email: email,
},
data: {
username: name,
},
});
}
}
res.json({ ...newUser, hasUsername });
console.log("User created successfully:", newUser);
} catch (error) {
console.error("User creation failed:", error);
res.status(500).send("User creation failed");
}
});
// Route to get user information
app.get("/user/me", async (req, res) => {
const token = req.headers.authorization;
console.log({ token });
if (!token) {
return res.status(401).send("Unauthorized: No token provided");
}
try {
// Verify the Firebase token
const decodedToken = await getAuth().verifyIdToken(token);
const { email } = decodedToken;
const user = await db.user.findFirst({
where: {
email: email,
},
});
if (!user) {
return res.status(404).send("User not found");
}
res.json({
id: user.id,
email: user.email,
name: user.username,
profilePic: user.profilePic,
});
} catch (error) {
console.error("Failed to fetch user data:", error);
res.status(500).send("Failed to fetch user data");
}
});
app.use(productRoute);
app.use(categoryRoute);
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/upload", uploadImages, async (req, res) => {
try {
const files = req.files as Express.Multer.File[];
if (!files) {
return res.status(400).send("No files uploaded.");
}
const processedImages = await Promise.all(
files.map((file) => {
const outputPath = `./uploads/${file.filename}`;
return sharp(file.path).resize(500, 500).toFile(outputPath);
})
);
res.json({
message: "Images uploaded and resized successfully",
files: processedImages,
});
} catch (error) {
console.error("Error processing images:", error);
res.status(500).send("Error processing images");
}
});
app.listen(3002, () => {
console.log("Server is running on port http://localhost:3002");
});
export { app };