-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.controller.js
600 lines (509 loc) · 14.7 KB
/
user.controller.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import { asyncHandler } from "../utils/asyncHandler.js";
import { ApiError } from "../utils/ApiError.js";
import { User } from "../models/user.model.js";
import {
deleteOldFileInCloudinary,
uploadOnCloudinary,
} from "../utils/cloudinaryFileUpload.js";
import { ApiResponse } from "../utils/ApiResponse.js";
import jwt from "jsonwebtoken";
import mongoose from "mongoose";
const generateAccessAndRefreshTokens = async (userId) => {
try {
const user = await User.findById(userId);
const accessToken = user.generateAccessToken();
const newRefreshToken = user.generateRefreshToken();
user.refreshToken = newRefreshToken;
await user.save({ validateBeforeSave: false });
return { accessToken, newRefreshToken };
} catch (error) {
throw new ApiError(
500,
"Something went wrong while generating refresh and access tokens."
);
}
};
const registerUser = asyncHandler(async (req, res) => {
//? Sample response from server
// res.status(200).json({
// message: "ok Kunal"
// });
/**
* !This are the steps for registering a user
* ? 1. Get the user details from frontend
* ? 2. Validate the user details are not empty
* ? 3. Check the user which is already registered or not: username, email
* ? 4. Check for user image and avatar
* ? 5. Upload them to cloudinary: avatar
* ? 6. Create a user object - create a entry in db
* ? 7. Remove password and refresh token from the response
* ? 8. Check for user creation
* ? 9. return response
*/
const { fullName, username, email, password } = req.body;
// console.log("Email: ", email);
// console.log("req.body -> ", req.body);
// ! Ye hai aam jindagi
// if (fullName === "") {
// throw new ApiError(400, "fullName is required");
// } // and so on ....
//? Ye hai Mentos jindagi
if (
[fullName, username, email, password].some(
(field) => field?.trim() === "" || field?.trim() === undefined
)
) {
throw new ApiError(400, `${field} is required`);
}
// Checking if user is already registered
const userExist = await User.findOne({
$or: [{ username }, { email }],
});
// console.log("userExist -> ", userExist);
if (userExist) {
throw new ApiError(409, "User with username or email already exists");
}
// console.log("req.files -> ", req.files);
const avatarLocalPath = req.files?.avatar[0]?.path;
// const coverImageLocalPath = req.files?.coverImage[0]?.path;
// NOTE: In this when we didn't have coverImage then it throw error like 'Cannot read properties of undefined'
// ! So we can do it in this method we can also do for avatar
let coverImageLocalPath;
if (
req.files &&
Array.isArray(req.files.coverImage) &&
req.files.coverImage.length > 0
) {
coverImageLocalPath = req.files.coverImage[0].path;
}
// console.log("avatarLocalPath ", avatarLocalPath);
if (!avatarLocalPath) {
throw new ApiError(400, "Avatar file is required");
}
const avatar = await uploadOnCloudinary(avatarLocalPath);
const coverImage = await uploadOnCloudinary(coverImageLocalPath);
if (!avatar) {
throw new ApiError(400, "Avatar file is required");
}
// console.log("avatar -> ", avatar);
const user = await User.create({
fullName,
avatar: avatar.url,
coverImage: coverImage?.url || "",
email,
password,
username: username.toLowerCase(),
});
const userCreated = await User.findById(user._id).select(
"-password -refreshToken"
);
if (!userCreated) {
throw new ApiError(500, "Something went wrong while creating the user");
}
return res
.status(201)
.json(new ApiResponse(200, userCreated, "User register successfully"));
});
const loginUser = asyncHandler(async (req, res) => {
/**
* !This are the steps for registering a user
* ? 1. get user data from req.body
* ? 2. get the user credentials -> username and email
* ? 3. find the user
* ? 4. password check
* ? 5. give refresh token and access token
* ? 6. send it in secure cookies
*/
const { username, email, password } = req.body;
// res.status(200).json({
// message: "user login successful",
// data: {
// username,
// email,
// password,
// },
// });
// if (!(username || email)) {
// throw new ApiError(400, " Username or email required");
// }
if (!username && !email) {
throw new ApiError(400, " Username or email required");
}
const user = await User.findOne({
$or: [{ username }, { email }],
});
if (!user) {
throw new ApiError(404, "User not found");
}
const isPasswordValid = await user.isPasswordCorrect(password);
if (!isPasswordValid) {
throw new ApiError(401, "Passwords do not match");
}
const { accessToken, newRefreshToken } = await generateAccessAndRefreshTokens(
user._id
);
const loggedInUser = await User.findById(user._id).select(
"-password -refreshToken"
);
// NOTE: In frontend mode cookie can modify but using this it modifies by server only
const options = {
httpOnly: true,
secure: true,
};
return res
.status(200)
.cookie("accessToken", accessToken, options)
.cookie("refreshToken", newRefreshToken, options)
.json(
new ApiResponse(
200,
{
user: loggedInUser,
accessToken,
refreshToken: newRefreshToken,
},
"User logged in successfully"
)
);
// console.log(`User: ${username} email: ${email} password: ${password}`);
});
const logoutUser = asyncHandler(async (req, res) => {
User.findByIdAndUpdate(
req.user._id,
{
$unset: {
refreshToken: 1, // This will remove the field from the document
},
},
{
new: true,
}
);
const options = {
httpOnly: true,
secure: true,
};
return res
.status(200)
.clearCookie("accessToken", options)
.clearCookie("refreshToken", options)
.json(new ApiResponse(200, {}, "User logged out"));
});
const refreshAccessToken = asyncHandler(async (req, res) => {
const incomingRefreshToken = req.user.refreshToken;
if (!incomingRefreshToken) {
throw new ApiError(401, "Unauthorized request");
}
try {
// console.log("incomingRefreshToken ", incomingRefreshToken);
const decodedToken = jwt.verify(
incomingRefreshToken,
process.env.REFRESH_TOKEN_SECRET
);
// console.log(" Decoded token: ", decodedToken);
const user = await User.findById(decodedToken?._id);
if (!user) {
throw new ApiError(401, "Invalid refresh token");
}
if (incomingRefreshToken !== user?.refreshToken) {
throw new ApiError(401, "Refresh token is expired or used");
}
const options = {
httpOnly: true,
secure: true,
};
const { accessToken, newRefreshToken } =
await generateAccessAndRefreshTokens(user._id);
return res
.status(200)
.cookie("accessToken", accessToken, options)
.cookie("refreshToken", newRefreshToken, options)
.json(
new ApiResponse(
200,
{
accessToken,
refreshToken: newRefreshToken,
},
"Access token refreshed successfully"
)
);
} catch (error) {
throw new ApiError(401, error?.message || "Invalid refresh token");
}
});
const changeCurrentUserPassword = asyncHandler(async (req, res) => {
const { oldPassword, newPassword } = req.body;
const user = await User.findById(req.user?._id);
const isPasswordValid = await user.isPasswordCorrect(oldPassword);
console.log("isPasswordValid", isPasswordValid);
if (!isPasswordValid) {
throw new ApiError(400, "Invalid password");
}
user.password = newPassword;
await user.save({ validateBeforeSave: false });
return res
.status(200)
.json(new ApiResponse(200, {}, "Password updated successfully"));
});
/** */
const getCurrentUser = asyncHandler(async (req, res) => {
// const user = User.findById(req.user?._id)
return res
.status(200)
.json(new ApiResponse(200, req.user, "Current user fetched successfully"));
});
// NOTE: In this we can use avatar and coverImage but it always better to separate controller and end point for the particular file update
const updatedUserDetails = asyncHandler(async (req, res) => {
const { fullName, username, email } = req.body;
if (!(fullName || username || email)) {
throw new ApiError(400, "All files are required");
}
const user = await User.findByIdAndUpdate(
req.user?._id,
{
$set: {
username,
fullName,
email,
},
},
{
new: true,
}
).select("-password");
return res
.status(200)
.json(new ApiResponse(200, user, "Account details updated successfully"));
});
/**
*
* NOTE: In this we 2 middleware
* ? 1. Multer -> for file accepting
* ? 2. verifyJWT(own) -> for check user is logged in or not
*
* */
const updateUserAvatar = asyncHandler(async (req, res) => {
const avatarLocalPath = req.file?.path;
if (!avatarLocalPath) {
throw new ApiError(401, "Avatar file is missing");
}
// console.log("avatarLocalPath ", avatarLocalPath);
// FIXED: delete old image which is on cloudinary
const oldAvatar = req.user.avatar;
// console.log("oldAvatar ", oldAvatar);
const avatar = await uploadOnCloudinary(avatarLocalPath);
console.log(avatar);
if (!avatar.url) {
throw new ApiError(500, "Something went wrong while uploading avatar");
}
const user = await User.findByIdAndUpdate(
req.user?._id,
{
$set: {
// avatar: { publicId: avatar.public_id, url: avatar.url },
avatar: avatar.url,
},
},
{
new: true,
}
).select("-password -refreshToken");
if (!user) {
throw new ApiError(500, "Something went wrong or User not found");
}
try {
const isOldImageDelete = await deleteOldFileInCloudinary(
oldAvatar
);
console.log("isOldImageDelete ", isOldImageDelete);
} catch (error) {
console.log("error - ", error);
}
return res
.status(200)
.json(new ApiResponse(200, user, "Avatar updated successfully"));
});
const updateUserCoverImage = asyncHandler(async (req, res) => {
const coverImageLocalPath = req.file?.path;
const oldCoverImage = req.user.coverImage;
/**
* FIXED: check
* 1) if user have not already cover image and it update what the value of oldCoverImage this.
* 2) if it give error then handle it properly
*/
if (!oldCoverImage) {
throw new ApiError(401, "Cover image not found");
}
if (!coverImageLocalPath) {
throw new ApiError(400, "CoverImage file is missing");
}
const coverImage = await uploadOnCloudinary(coverImageLocalPath);
if (!coverImage.url) {
throw new ApiError(500, "Something went wrong while uploading cover image");
}
const user = await User.findByIdAndUpdate(
req.user?._id,
{
$set: {
// coverImage: {
// publicId: coverImage?.public_id || "",
// url: coverImage?.url || "",
// },
coverImage: coverImage?.url,
},
},
{ new: true }
);
if (!oldCoverImage === "") {
try {
const isOldImageDelete = await deleteOldFileInCloudinary(
oldCoverImage
);
console.log("isOldImageDelete ", isOldImageDelete);
} catch (error) {
console.log("error - ", error);
}
}
return res
.status(200)
.json(new ApiResponse(200, user, "CoverImage updated successfully"));
});
const getUserChannelProfile = asyncHandler(async (req, res) => {
const { username } = req.params;
if (!username?.trim()) {
throw new ApiError(400, "User is missing");
}
const channel = await User.aggregate([
// First pipeline -> check user match or not
{
$match: {
username: username?.toLowerCase(),
},
},
// Second pipeline -> for user which channel is subscribers
{
$lookup: {
from: "subscriptions",
localField: "_id",
foreignField: "channel",
as: "subscribers",
},
},
// Third pipeline -> for channel which the user subscribed
{
$lookup: {
from: "subscriptions",
localField: "_id",
foreignField: "subscriber",
as: "subscribedTo",
},
},
// Fourth pipeline -> for checking count of subscribers and channels
{
$addFields: {
subscriberCount: {
$size: "$subscribers",
},
channelSubscribedToCount: {
$size: "$subscribedTo",
},
isSubscribed: {
$cond: {
if: { $in: [req.user?._id, "$subscribers.subscriber"] },
then: true,
else: false,
},
},
},
},
// Fifth pipeline -> for projection which gives only selected value
{
$project: {
username: 1,
fullName: 1,
avatar: 1,
coverImage: 1,
email: 1,
subscriberCount: 1,
channelSubscribedToCount: 1,
isSubscribed: 1,
},
},
]);
if (!channel?.length) {
throw new ApiError(404, "Channel does not exist");
}
console.log(" The output data from the pipeline", channel);
return res
.status(200)
.json(
new ApiResponse(200, channel[0], "User channel fetched successfully.")
);
});
const getWatchHistory = asyncHandler(async (req, res) => {
const user = await User.aggregate([
{
$match: {
_id: new mongoose.Types.ObjectId(req.user._id), // NOTE: In this the _id gives us an ObjectId as string so want to write like this because this cannot transform into that value
},
},
{
$lookup: {
from: "videos",
localField: "watchHistory",
foreignField: "_id",
as: "watchHistory",
pipeline: [
{
$lookup: {
from: "users",
localField: "owner",
foreignField: "_id",
as: "owner",
// TODO: In this you change the owner field in second stage of the pipeline TRY ONCE
pipeline: [
{
$project: {
username: 1,
fullName: 1,
avatar: 1,
},
},
],
},
},
{
$addFields: {
owner: {
$first: "$owner",
},
},
},
],
},
},
]);
return res
.status(200)
.json(
new ApiResponse(
200,
user[0].watchHistory,
"Watch history fetched successfully"
)
);
});
export {
registerUser,
loginUser,
logoutUser,
refreshAccessToken,
changeCurrentUserPassword,
getCurrentUser,
updatedUserDetails,
updateUserAvatar,
updateUserCoverImage,
getUserChannelProfile,
getWatchHistory,
};