Skip to content

Commit a2f0ce7

Browse files
committed
feat: Add Swagger documentation for API endpoints and integrate Swagger UI
1 parent 4680406 commit a2f0ce7

7 files changed

Lines changed: 566 additions & 14 deletions

File tree

server/configs/swagger.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import swaggerJsdoc from "swagger-jsdoc";
2+
3+
const options = {
4+
definition: {
5+
openapi: "3.0.0",
6+
info: {
7+
title: "Chatzy API",
8+
version: "1.0.0",
9+
description: "API documentation for Chatzy chat application",
10+
contact: {
11+
name: "API Support",
12+
email: "suyash.2023ug1100@iiitranchi.ac.in",
13+
},
14+
license: {
15+
name: "GNU General Public License v3.0",
16+
url: "https://www.gnu.org/licenses/gpl-3.0.html",
17+
},
18+
},
19+
servers: [
20+
{
21+
url:
22+
process.env.NODE_ENV === "production"
23+
? "https://chatzy-mxp8.onrender.com/"
24+
: `http://localhost:${process.env.PORT || 5000}`,
25+
description:
26+
process.env.NODE_ENV === "production"
27+
? "Production server"
28+
: "Development server",
29+
},
30+
],
31+
components: {
32+
securitySchemes: {
33+
cookieAuth: {
34+
type: "apiKey",
35+
in: "cookie",
36+
name: "token",
37+
},
38+
},
39+
},
40+
},
41+
apis: ["./routes/*.js"],
42+
};
43+
44+
export const swaggerSpec = swaggerJsdoc(options);

server/controllers/authController.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { generateToken } from "../configs/utils.js";
22
import User from "../models/user.js";
33
import bcrypt from "bcryptjs";
44
import randomstring from "randomstring";
5-
import * as brevo from '@getbrevo/brevo';
5+
import * as brevo from "@getbrevo/brevo";
66
import { v2 as cloudinary } from "cloudinary";
77
import { extractPublicId } from "cloudinary-build-url";
88
import dotenv, { config } from "dotenv";
@@ -103,29 +103,36 @@ export const updateProfilePic = async (req, res) => {
103103
try {
104104
const userId = req.user._id;
105105
const { profilePic } = req.body;
106+
106107
if (!profilePic) {
107108
return res
108109
.status(400)
109110
.json({ message: "Please provide a profile picture" });
110111
}
112+
111113
const existingUser = await User.findById({ _id: userId });
114+
112115
if (existingUser?.profilePic) {
113116
const publicId = extractPublicId(existingUser.profilePic);
114117
// console.log(publicId);
115118
const result = await cloudinary.uploader.destroy(publicId);
116119
}
120+
117121
let uploadImage = await cloudinary.uploader.upload(profilePic, {
118122
folder: "chatzy/profile_pictures",
119123
});
124+
120125
const user = await User.findByIdAndUpdate(
121126
userId,
122127
{ profilePic: uploadImage.secure_url },
123128
{ new: true }
124129
);
130+
125131
await user.save();
126132
res
127133
.status(200)
128134
.json({ user, message: "Profile Picture Updated Successfully!" });
135+
129136
} catch (e) {
130137
console.log(e.message);
131138
res.status(500).json({
@@ -139,19 +146,24 @@ export const deleteProfilePic = async (req, res) => {
139146
if (!req.user.profilePic) {
140147
return res.status(400).json({ message: "No profile picture to delete" });
141148
}
149+
142150
const publicId = extractPublicId(req.user.profilePic);
143151
// console.log(publicId);
152+
144153
const result = await cloudinary.uploader.destroy(publicId);
145154
const userId = req.user._id;
155+
146156
const user = await User.findByIdAndUpdate(
147157
userId,
148158
{ profilePic: "" },
149159
{ new: true }
150160
);
161+
151162
await user.save();
152163
res
153164
.status(200)
154165
.json({ user, message: "Profile Picture Deleted Successfully!" });
166+
155167
} catch (e) {
156168
console.log(e.message);
157169
res.status(500).json({
@@ -188,19 +200,19 @@ async function sendOtp(email, otp) {
188200
const sendSmtpEmail = new brevo.SendSmtpEmail();
189201
sendSmtpEmail.subject = "OTP Verification";
190202
sendSmtpEmail.to = [{ email: email }];
191-
sendSmtpEmail.sender = {
192-
name: "OTP Authentication",
193-
email: process.env.MAIL_USER
203+
sendSmtpEmail.sender = {
204+
name: "OTP Authentication",
205+
email: process.env.MAIL_USER,
194206
};
195207
sendSmtpEmail.textContent = `Your OTP for verification is ${otp}. DO NOT share it with anyone.`;
196208

197209
const result = await apiInstance.sendTransacEmail(sendSmtpEmail);
198-
console.log('OTP sent successfully via Brevo:', result?.body?.messageId);
210+
console.log("OTP sent successfully via Brevo:", result?.body?.messageId);
199211
return true;
200212
} catch (error) {
201-
console.error('Brevo OTP sending failed:', error.message);
213+
console.error("Brevo OTP sending failed:", error.message);
202214
if (error.response) {
203-
console.error('Brevo error details:', error.response.body);
215+
console.error("Brevo error details:", error.response.body);
204216
}
205217
return false;
206218
}
@@ -212,13 +224,13 @@ export const getOtp = async (req, res) => {
212224
console.log("Getting OTP for email:", email);
213225
console.log("BREVO_API_KEY set:", !!process.env.BREVO_API_KEY);
214226
console.log("MAIL_USER set:", !!process.env.MAIL_USER);
215-
227+
216228
const otp = generateOtp();
217229
otpCache[email] = await bcrypt.hash(otp, 10);
218230

219231
const result = await sendOtp(email, otp);
220232
console.log("Send OTP result:", result);
221-
233+
222234
if (result) {
223235
res.cookie("otpCache", otpCache, {
224236
maxAge: 300000,
@@ -241,14 +253,14 @@ export const getOtp = async (req, res) => {
241253
export const verifyOtp = async (req, res) => {
242254
const { formData, givenOTP } = req.body;
243255
const actualOTPCache = req.cookies.otpCache;
244-
256+
245257
if (!actualOTPCache) {
246258
return res.status(400).json({ message: "OTP expired." });
247259
}
248260
if (!actualOTPCache.hasOwnProperty(formData.email)) {
249261
return res.status(400).json({ message: "Email not found, try again" });
250262
}
251-
263+
252264
const decodedOtp = await bcrypt.compare(
253265
givenOTP.trim(),
254266
actualOTPCache[formData.email]
@@ -263,4 +275,4 @@ export const verifyOtp = async (req, res) => {
263275
} else {
264276
return res.status(400).json({ message: "Invalid OTP" });
265277
}
266-
};
278+
};

0 commit comments

Comments
 (0)