@imksh/create 1.0.31 → 1.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/README.md +2 -0
  2. package/package.json +1 -1
  3. package/src/commands/create.js +2 -0
  4. package/src/index.js +1 -1
  5. package/templates/layers/next/javascript/package.json +8 -2
  6. package/templates/layers/next/typescript/.eslintrc.json +12 -0
  7. package/templates/layers/next/typescript/package.json +14 -3
  8. package/templates/layers/next/typescript/src/components/common/Loading.tsx +1 -1
  9. package/templates/layers/next/typescript/src/components/common/LottieIcon.tsx +2 -2
  10. package/templates/layers/next/typescript/src/components/common/Table.tsx +4 -4
  11. package/templates/layers/next/typescript/src/components/modals/ConfirmModal.tsx +2 -1
  12. package/templates/layers/next/typescript/src/components/ui/Header.tsx +2 -2
  13. package/templates/layers/next/typescript/src/hooks/usePwaInstall.ts +10 -4
  14. package/templates/layers/next/typescript/src/services/authService.ts +1 -1
  15. package/templates/layers/next/typescript/src/store/useAuthStore.ts +24 -9
  16. package/templates/layers/node/javascript/.env.example +3 -1
  17. package/templates/layers/node/javascript/package.json +12 -3
  18. package/templates/layers/node/javascript/src/app.js +2 -1
  19. package/templates/layers/node/javascript/src/controllers/auth.controller.js +106 -168
  20. package/templates/layers/node/javascript/src/controllers/push.controller.js +51 -58
  21. package/templates/layers/node/javascript/src/controllers/upload.controller.js +12 -19
  22. package/templates/layers/node/javascript/src/middleware/auth.middleware.js +5 -11
  23. package/templates/layers/node/javascript/src/middleware/errorHandler.js +6 -4
  24. package/templates/layers/node/javascript/src/routes/auth.route.js +2 -0
  25. package/templates/layers/node/javascript/src/services/auth.service.js +159 -164
  26. package/templates/layers/node/javascript/src/utils/ApiError.js +20 -0
  27. package/templates/layers/node/javascript/src/utils/SuccessResponse.js +15 -0
  28. package/templates/layers/node/javascript/src/utils/apiResponse.js +4 -0
  29. package/templates/layers/node/javascript/src/utils/asyncHandler.js +5 -0
  30. package/templates/layers/node/typescript/.env.example +3 -1
  31. package/templates/layers/node/typescript/.eslintrc.cjs +28 -0
  32. package/templates/layers/node/typescript/package.json +16 -2
  33. package/templates/layers/node/typescript/src/app.ts +3 -2
  34. package/templates/layers/node/typescript/src/config/db.ts +3 -2
  35. package/templates/layers/node/typescript/src/config/socket.ts +5 -4
  36. package/templates/layers/node/typescript/src/controllers/auth.controller.ts +104 -132
  37. package/templates/layers/node/typescript/src/controllers/push.controller.ts +52 -59
  38. package/templates/layers/node/typescript/src/controllers/upload.controller.ts +12 -19
  39. package/templates/layers/node/typescript/src/middleware/auth.middleware.ts +13 -7
  40. package/templates/layers/node/typescript/src/middleware/errorHandler.ts +18 -5
  41. package/templates/layers/node/typescript/src/middleware/rateLimiter.ts +2 -2
  42. package/templates/layers/node/typescript/src/routes/auth.route.ts +2 -0
  43. package/templates/layers/node/typescript/src/services/auth.service.ts +86 -43
  44. package/templates/layers/node/typescript/src/types/express.d.ts +5 -3
  45. package/templates/layers/node/typescript/src/utils/ApiError.ts +25 -0
  46. package/templates/layers/node/typescript/src/utils/SuccessResponse.ts +17 -0
  47. package/templates/layers/node/typescript/src/utils/apiResponse.ts +4 -0
  48. package/templates/layers/node/typescript/src/utils/asyncHandler.ts +13 -0
  49. package/templates/layers/node/typescript/src/utils/cloudinaryUpload.ts +18 -7
  50. package/templates/layers/node/typescript/src/utils/expoNotification.ts +1 -1
  51. package/templates/layers/node/typescript/src/utils/genOtpToken.ts +7 -1
  52. package/templates/layers/node/typescript/src/utils/jwt.ts +8 -2
  53. package/templates/layers/node/typescript/src/utils/logger.ts +2 -1
  54. package/templates/layers/node/typescript/src/utils/pushHelper.ts +13 -9
  55. package/templates/layers/node-prisma/javascript/.env.example +3 -1
  56. package/templates/layers/node-prisma/javascript/package.json +11 -2
  57. package/templates/layers/node-prisma/javascript/src/app.js +2 -1
  58. package/templates/layers/node-prisma/javascript/src/controllers/auth.controller.js +90 -135
  59. package/templates/layers/node-prisma/javascript/src/controllers/push.controller.js +64 -76
  60. package/templates/layers/node-prisma/javascript/src/controllers/upload.controller.js +12 -19
  61. package/templates/layers/node-prisma/javascript/src/middleware/auth.middleware.js +5 -4
  62. package/templates/layers/node-prisma/javascript/src/middleware/errorHandler.js +6 -4
  63. package/templates/layers/node-prisma/javascript/src/routes/auth.route.js +2 -0
  64. package/templates/layers/node-prisma/javascript/src/services/auth.service.js +145 -122
  65. package/templates/layers/node-prisma/javascript/src/utils/ApiError.js +20 -0
  66. package/templates/layers/node-prisma/javascript/src/utils/SuccessResponse.js +15 -0
  67. package/templates/layers/node-prisma/javascript/src/utils/apiResponse.js +4 -0
  68. package/templates/layers/node-prisma/javascript/src/utils/asyncHandler.js +5 -0
  69. package/templates/layers/node-prisma/typescript/.env.example +3 -1
  70. package/templates/layers/node-prisma/typescript/.eslintrc.cjs +28 -0
  71. package/templates/layers/node-prisma/typescript/package.json +19 -5
  72. package/templates/layers/node-prisma/typescript/src/app.ts +3 -2
  73. package/templates/layers/node-prisma/typescript/src/config/prisma.ts +3 -2
  74. package/templates/layers/node-prisma/typescript/src/config/socket.ts +5 -4
  75. package/templates/layers/node-prisma/typescript/src/controllers/auth.controller.ts +89 -105
  76. package/templates/layers/node-prisma/typescript/src/controllers/push.controller.ts +67 -83
  77. package/templates/layers/node-prisma/typescript/src/controllers/upload.controller.ts +12 -19
  78. package/templates/layers/node-prisma/typescript/src/middleware/auth.middleware.ts +9 -13
  79. package/templates/layers/node-prisma/typescript/src/middleware/errorHandler.ts +18 -5
  80. package/templates/layers/node-prisma/typescript/src/middleware/rateLimiter.ts +2 -2
  81. package/templates/layers/node-prisma/typescript/src/routes/auth.route.ts +2 -0
  82. package/templates/layers/node-prisma/typescript/src/services/auth.service.ts +83 -31
  83. package/templates/layers/node-prisma/typescript/src/types/express.d.ts +5 -3
  84. package/templates/layers/node-prisma/typescript/src/utils/ApiError.ts +25 -0
  85. package/templates/layers/node-prisma/typescript/src/utils/SuccessResponse.ts +17 -0
  86. package/templates/layers/node-prisma/typescript/src/utils/apiResponse.ts +4 -0
  87. package/templates/layers/node-prisma/typescript/src/utils/asyncHandler.ts +13 -0
  88. package/templates/layers/node-prisma/typescript/src/utils/cloudinaryUpload.ts +18 -7
  89. package/templates/layers/node-prisma/typescript/src/utils/genOtpToken.ts +7 -1
  90. package/templates/layers/node-prisma/typescript/src/utils/jwt.ts +8 -2
  91. package/templates/layers/node-prisma/typescript/src/utils/logger.ts +2 -1
  92. package/templates/layers/node-prisma/typescript/src/utils/pushHelper.ts +10 -6
  93. package/templates/layers/prisma/javascript/package.json +8 -0
  94. package/templates/layers/prisma/typescript/package.json +14 -1
  95. package/templates/layers/prisma/typescript/src/config/db.ts +3 -2
  96. package/templates/layers/react/javascript/package.json +6 -0
  97. package/templates/layers/react/typescript/.eslintrc.cjs +20 -0
  98. package/templates/layers/react/typescript/package.json +11 -2
  99. package/templates/layers/react/typescript/src/components/common/Table.tsx +4 -4
  100. package/templates/layers/react/typescript/src/components/ui/Sidebar.tsx +1 -1
  101. package/templates/layers/react/typescript/src/main.tsx +11 -11
  102. package/templates/layers/react/typescript/src/services/authService.ts +1 -1
  103. package/templates/layers/react/typescript/src/store/useAuthStore.ts +24 -9
  104. package/templates/layers/react-native/base/app.json +9 -22
  105. package/templates/layers/react-native/base/eslint.config.js +5 -1
  106. package/templates/layers/react-native/base/package.json +40 -33
  107. package/templates/layers/react-native/base/src/app/(auth)/edit-profile.tsx +7 -7
  108. package/templates/layers/react-native/base/src/app/(auth)/login.tsx +31 -7
  109. package/templates/layers/react-native/base/src/app/(tabs)/_layout.tsx +3 -3
  110. package/templates/layers/react-native/base/src/app/(tabs)/index.tsx +6 -5
  111. package/templates/layers/react-native/base/src/app/_layout.tsx +4 -8
  112. package/templates/layers/react-native/base/src/app/screens/doc-viewer.tsx +378 -0
  113. package/templates/layers/react-native/base/src/components/common/ConfirmationToast.tsx +58 -40
  114. package/templates/layers/react-native/base/src/components/common/GoogleAuthButton.tsx +123 -0
  115. package/templates/layers/react-native/base/src/components/common/LocationLoader.tsx +64 -9
  116. package/templates/layers/react-native/base/src/components/common/Typography.tsx +54 -19
  117. package/templates/layers/react-native/base/src/components/ui/BiometricLock.tsx +3 -3
  118. package/templates/layers/react-native/base/src/components/ui/ClassicTabBar.tsx +15 -12
  119. package/templates/layers/react-native/base/src/components/ui/CustomTabBar.tsx +90 -91
  120. package/templates/layers/react-native/base/src/components/ui/Drawer.tsx +344 -0
  121. package/templates/layers/react-native/base/src/components/ui/Sidebar.tsx +4 -4
  122. package/templates/layers/react-native/base/src/components/ui/buttons/Button.tsx +29 -6
  123. package/templates/layers/react-native/base/src/components/ui/data-display/Accordion.tsx +2 -2
  124. package/templates/layers/react-native/base/src/components/ui/data-display/Badge.tsx +1 -2
  125. package/templates/layers/react-native/base/src/components/ui/data-display/Image.tsx +3 -3
  126. package/templates/layers/react-native/base/src/components/ui/data-display/SwipeableRow.tsx +28 -7
  127. package/templates/layers/react-native/base/src/components/ui/feedback/GestureWrapper.tsx +11 -4
  128. package/templates/layers/react-native/base/src/components/ui/inputs/DatePicker.tsx +3 -3
  129. package/templates/layers/react-native/base/src/components/ui/inputs/ImagePickerModal.tsx +45 -19
  130. package/templates/layers/react-native/base/src/components/ui/inputs/Input.tsx +28 -9
  131. package/templates/layers/react-native/base/src/components/ui/inputs/SearchablePicker.tsx +199 -0
  132. package/templates/layers/react-native/base/src/components/ui/inputs/Switch.tsx +7 -7
  133. package/templates/layers/react-native/base/src/components/ui/inputs/WebLocationPicker.tsx +146 -72
  134. package/templates/layers/react-native/base/src/config/api.ts +5 -5
  135. package/templates/layers/react-native/base/src/services/auth.service.ts +3 -1
  136. package/templates/layers/react-native/base/src/services/permission.service.ts +168 -44
  137. package/templates/layers/react-native/base/src/services/socket.service.ts +0 -7
  138. package/templates/layers/react-native/base/src/store/useAuthStore.ts +61 -16
  139. package/templates/layers/react-native/base/src/store/useNotificationStore.ts +36 -42
  140. package/templates/layers/react-native/base/src/types/images.d.ts +7 -5
  141. package/templates/layers/react-native/base/src/utils/env.ts +47 -0
  142. package/templates/layers/react-native/base/src/utils/fileDownload.ts +206 -0
  143. package/templates/layers/react-native/base/src/utils/storage.ts +1 -1
  144. package/templates/presets/production-fullstack/overrides/client/package.json +9 -0
  145. package/templates/presets/production-fullstack/overrides/mobile/app.json +2 -15
  146. package/templates/presets/production-fullstack/overrides/mobile/package.json +39 -29
  147. package/templates/presets/production-fullstack/overrides/mobile/src/app/_layout.tsx +3 -3
  148. package/templates/presets/production-fullstack/overrides/mobile/src/components/ui/Drawer.tsx +344 -0
  149. package/templates/presets/production-fullstack/overrides/mobile/src/components/ui/inputs/DatePicker.tsx +3 -3
  150. package/templates/presets/production-fullstack/overrides/mobile/src/components/ui/inputs/ImagePickerModal.tsx +3 -3
  151. package/templates/presets/production-fullstack/overrides/mobile/src/services/permission.service.ts +168 -44
  152. package/templates/presets/production-fullstack/overrides/mobile/src/store/useNotificationStore.ts +16 -0
  153. package/templates/presets/production-fullstack/overrides/mobile/src/utils/env.ts +47 -0
  154. package/templates/presets/production-fullstack/overrides/server/package.json +11 -2
  155. package/templates/layers/react-native/base/src/app/doc-viewer.tsx +0 -193
  156. package/templates/layers/react-native/base/src/components/ui/BottomModal.tsx +0 -176
  157. package/templates/presets/production-fullstack/overrides/mobile/src/components/ui/BottomModal.tsx +0 -159
  158. /package/templates/layers/react-native/base/assets/images/logo/{logoWithBg.png → icon.png} +0 -0
@@ -1,181 +1,119 @@
1
1
  import * as authService from "../services/auth.service.js";
2
2
  import { generateToken } from "../utils/jwt.js";
3
3
  import genOtpToken from "../utils/genOtpToken.js";
4
- import logger from "../utils/logger.js";
5
-
6
- export const signup = async (req, res, next) => {
7
- try {
8
- const { name, email, phone, password, otp } = req.body;
9
-
10
- if (!email || !password || !name || !otp) {
11
- return next({ status: 400, message: "All fields are required." });
12
- }
13
-
14
- const newUser = await authService.signup({ name, email, phone, password, otp });
15
- const token = await generateToken(newUser, res);
16
- const safeUser = newUser.toObject();
17
- delete safeUser.password;
18
-
19
- res
20
- .status(201)
21
- .json({ message: "Registration Successful", data: safeUser, token });
22
- } catch (error) {
23
- logger.error("Error in signup controller", error);
24
- next(error);
4
+ import { asyncHandler } from "../utils/asyncHandler.js";
5
+ import { SuccessResponse } from "../utils/SuccessResponse.js";
6
+ import { ApiError } from "../utils/ApiError.js";
7
+
8
+ export const signup = asyncHandler(async (req, res) => {
9
+ const { name, email, phone, password, otp } = req.body;
10
+ if (!email || !password || !name || !otp) {
11
+ throw new ApiError(400, "All fields are required.");
25
12
  }
26
- };
27
-
28
- export const login = async (req, res, next) => {
29
- try {
30
- const { email, password } = req.body;
31
- if (!email || !password) {
32
- return next({
33
- status: 400,
34
- message: "All fields are required.",
35
- });
36
- }
37
-
38
- const user = await authService.login({ email, password });
39
- const token = await generateToken(user, res);
40
- const safeUser = user.toObject();
41
- delete safeUser.password;
42
-
43
- res
44
- .status(200)
45
- .json({ message: "Login successfully", data: safeUser, token });
46
- } catch (error) {
47
- logger.error("Error in login controller", error);
48
- next(error);
13
+ const newUser = await authService.signup({ name, email, phone, password, otp });
14
+ const token = await generateToken(newUser, res);
15
+ const safeUser = newUser.toObject();
16
+ delete safeUser.password;
17
+ SuccessResponse(res, 201, "Registration Successful", { user: safeUser, token });
18
+ });
19
+
20
+ export const login = asyncHandler(async (req, res) => {
21
+ const { email, password } = req.body;
22
+ if (!email || !password) {
23
+ throw new ApiError(400, "All fields are required.");
49
24
  }
50
- };
51
-
52
- export const logout = async (_req, res, next) => {
53
- try {
54
- res.clearCookie("token", {
55
- httpOnly: true,
56
- secure: process.env.NODE_ENV === "production",
57
- sameSite: "strict",
58
- path: "/",
59
- });
60
-
61
- res.status(200).json({ message: "Logout Successfully" });
62
- } catch (error) {
63
- logger.error("Error in logout controller", error);
64
- next(error);
25
+ const user = await authService.login({ email, password });
26
+ const token = await generateToken(user, res);
27
+ const safeUser = user.toObject();
28
+ delete safeUser.password;
29
+ SuccessResponse(res, 200, "Login successfully", { user: safeUser, token });
30
+ });
31
+
32
+ export const logout = asyncHandler(async (_req, res) => {
33
+ res.clearCookie("token", {
34
+ httpOnly: true,
35
+ secure: process.env.NODE_ENV === "production",
36
+ sameSite: "strict",
37
+ path: "/",
38
+ });
39
+ SuccessResponse(res, 200, "Logout Successfully");
40
+ });
41
+
42
+ export const getMe = asyncHandler(async (req, res) => {
43
+ const safeUser = typeof req.user?.toObject === "function" ? req.user.toObject() : { ...req.user };
44
+ delete safeUser.password;
45
+ SuccessResponse(res, 200, "User Authenticated", safeUser);
46
+ });
47
+
48
+ export const genSignupOtp = asyncHandler(async (req, res) => {
49
+ const { email } = req.body;
50
+ if (!email) {
51
+ throw new ApiError(400, "Email is required.");
65
52
  }
66
- };
67
-
68
- export const getMe = async (req, res, next) => {
69
- try {
70
- const safeUser = req.user.toObject();
71
- delete safeUser.password;
72
- res.status(200).json({ message: "User Authenticated", data: safeUser });
73
- } catch (error) {
74
- logger.error("Error in getMe controller", error);
75
- next(error);
53
+ await authService.generateSignupOtp(email);
54
+ SuccessResponse(res, 200, "Signup Otp sent successfully");
55
+ });
56
+
57
+ export const genOtp = asyncHandler(async (req, res) => {
58
+ const { email } = req.body;
59
+ if (!email) {
60
+ throw new ApiError(400, "Email is required.");
76
61
  }
77
- };
78
-
79
- export const genSignupOtp = async (req, res, next) => {
80
- try {
81
- const { email } = req.body;
82
-
83
- if (!email) {
84
- return next({ status: 400, message: "Email is required." });
85
- }
86
-
87
- await authService.generateSignupOtp(email);
88
- res.status(200).json({ message: "Signup Otp sent successfully" });
89
- } catch (error) {
90
- logger.error("Error in genSignupOtp", error);
91
- next(error);
62
+ await authService.generateOtp(email);
63
+ SuccessResponse(res, 200, "Otp sent successfully");
64
+ });
65
+
66
+ export const verifyOtp = asyncHandler(async (req, res) => {
67
+ const { email, otp } = req.body;
68
+ if (!email || !otp) {
69
+ throw new ApiError(400, "All fields are required.");
92
70
  }
93
- };
94
-
95
- export const genOtp = async (req, res, next) => {
96
- try {
97
- const { email } = req.body;
98
- if (!email) {
99
- return next({
100
- status: 400,
101
- message: "Email is required.",
102
- });
103
- }
104
-
105
- await authService.generateOtp(email);
106
- res.status(200).json({ message: "Otp sent successfully" });
107
- } catch (error) {
108
- logger.error("Error in genOtp", error);
109
- next(error);
71
+ const user = await authService.verifyOtp({ email, otp });
72
+ await genOtpToken(user, res);
73
+ SuccessResponse(res, 200, "Otp verified successfully");
74
+ });
75
+
76
+ export const resetPassword = asyncHandler(async (req, res) => {
77
+ const { newPassword } = req.body;
78
+ const currentUser = req.user;
79
+ if (!newPassword) {
80
+ throw new ApiError(400, "All fields are required.");
110
81
  }
111
- };
112
-
113
- export const verifyOtp = async (req, res, next) => {
114
- try {
115
- const { email, otp } = req.body;
116
- if (!email || !otp) {
117
- return next({
118
- status: 400,
119
- message: "All fields are required.",
120
- });
121
- }
122
-
123
- const user = await authService.verifyOtp({ email, otp });
124
- await genOtpToken(user, res);
125
-
126
- res.status(200).json({ message: "Otp verified successfully" });
127
- } catch (error) {
128
- logger.error("Error in verifyOtp", error);
129
- next(error);
82
+ await authService.resetPassword(currentUser, newPassword);
83
+ SuccessResponse(res, 200, "Password updated successfully");
84
+ });
85
+
86
+ export const updateMe = asyncHandler(async (req, res) => {
87
+ const { name, phone } = req.body;
88
+ const user = await authService.updateProfile(req.user._id, {
89
+ name,
90
+ phone,
91
+ file: req.file,
92
+ });
93
+ const safeUser = user.toObject();
94
+ delete safeUser.password;
95
+ SuccessResponse(res, 200, "Profile updated successfully", safeUser);
96
+ });
97
+
98
+ export const savePushToken = asyncHandler(async (req, res) => {
99
+ const { token } = req.body;
100
+ if (!token) {
101
+ throw new ApiError(400, "Token is required.");
130
102
  }
131
- };
132
-
133
- export const resetPassword = async (req, res, next) => {
134
- try {
135
- const { newPassword } = req.body;
136
- const currentUser = req.user;
137
- if (!newPassword) {
138
- return next({
139
- status: 400,
140
- message: "All fields are required.",
141
- });
142
- }
143
-
144
- await authService.resetPassword(currentUser, newPassword);
145
- res.status(200).json({ message: "Password updated successfully" });
146
- } catch (error) {
147
- logger.error("Error in resetPassword", error);
148
- next(error);
103
+ await authService.savePushToken(req.user._id, token);
104
+ SuccessResponse(res, 200, "Push token saved successfully");
105
+ });
106
+
107
+ export const googleLogin = asyncHandler(async (req, res) => {
108
+ const { idToken } = req.body;
109
+ if (!idToken) {
110
+ throw new ApiError(400, "idToken is required.");
149
111
  }
150
- };
151
-
152
- export const updateMe = async (req, res, next) => {
153
- try {
154
- const { name, phone } = req.body;
155
- const user = await authService.updateProfile(req.user._id, {
156
- name,
157
- phone,
158
- file: req.file,
159
- });
160
- const safeUser = user.toObject();
112
+ const user = await authService.googleLogin(idToken);
113
+ const token = await generateToken(user, res);
114
+ const safeUser = typeof user.toObject === "function" ? user.toObject() : { ...user };
115
+ if (safeUser.password !== undefined) {
161
116
  delete safeUser.password;
162
- res.status(200).json({ success: true, data: safeUser });
163
- } catch (error) {
164
- logger.error("Error updating profile", error);
165
- next(error);
166
- }
167
- };
168
-
169
- export const savePushToken = async (req, res, next) => {
170
- try {
171
- const { token } = req.body;
172
- if (!token) {
173
- return next({ status: 400, message: "Token is required." });
174
- }
175
- await authService.savePushToken(req.user._id, token);
176
- res.status(200).json({ success: true, message: "Push token saved successfully" });
177
- } catch (error) {
178
- logger.error("Error saving push token", error);
179
- next(error);
180
117
  }
181
- };
118
+ SuccessResponse(res, 200, "Google Login successfully", { user: safeUser, token });
119
+ });
@@ -1,72 +1,65 @@
1
1
  import PushSubscription from "../models/pushSubscription.model.js";
2
- import logger from "../utils/logger.js";
2
+ import { asyncHandler } from "../utils/asyncHandler.js";
3
+ import { SuccessResponse } from "../utils/SuccessResponse.js";
4
+ import { ApiError } from "../utils/ApiError.js";
3
5
 
4
- export const subscribe = async (req, res, next) => {
5
- try {
6
- const subscription = req.body;
7
- const userId = req.user?._id || req.user?.id; // supports both mongoose and prisma IDs
8
-
9
- if (!subscription) {
10
- return next({ status: 400, message: "Invalid subscription payload" });
11
- }
6
+ export const subscribe = asyncHandler(async (req, res) => {
7
+ const subscription = req.body;
8
+ const userId = req.user?._id || req.user?.id; // supports both mongoose and prisma IDs
12
9
 
13
- const isExpo = !!subscription.token;
14
- const isWeb = !!(subscription.endpoint && subscription.keys);
10
+ if (!subscription) {
11
+ throw new ApiError(400, "Invalid subscription payload");
12
+ }
15
13
 
16
- if (!isExpo && !isWeb) {
17
- return next({ status: 400, message: "Missing token (EXPO) or endpoint/keys (WEB)" });
18
- }
14
+ const isExpo = !!subscription.token;
15
+ const isWeb = !!(subscription.endpoint && subscription.keys);
19
16
 
20
- if (isExpo) {
21
- const existingSub = await PushSubscription.findOne({ expoPushToken: subscription.token });
22
- if (existingSub) {
23
- if (userId && String(existingSub.user) !== String(userId)) {
24
- existingSub.user = userId; await existingSub.save();
25
- }
26
- return res.status(200).json({ success: true, message: "Already subscribed." });
27
- }
17
+ if (!isExpo && !isWeb) {
18
+ throw new ApiError(400, "Missing token (EXPO) or endpoint/keys (WEB)");
19
+ }
28
20
 
29
- await PushSubscription.create({
30
- type: "EXPO",
31
- expoPushToken: subscription.token,
32
- user: userId || undefined
33
- });
34
- } else {
35
- const existingSub = await PushSubscription.findOne({ endpoint: subscription.endpoint });
36
- if (existingSub) {
37
- if (userId && String(existingSub.user) !== String(userId)) {
38
- existingSub.user = userId; await existingSub.save();
39
- }
40
- return res.status(200).json({ success: true, message: "Already subscribed." });
21
+ if (isExpo) {
22
+ const existingSub = await PushSubscription.findOne({ expoPushToken: subscription.token });
23
+ if (existingSub) {
24
+ if (userId && String(existingSub.user) !== String(userId)) {
25
+ existingSub.user = userId;
26
+ await existingSub.save();
41
27
  }
28
+ return SuccessResponse(res, 200, "Already subscribed.");
29
+ }
42
30
 
43
- await PushSubscription.create({
44
- type: "WEB",
45
- endpoint: subscription.endpoint,
46
- keys: subscription.keys,
47
- user: userId || undefined
48
- });
31
+ await PushSubscription.create({
32
+ type: "EXPO",
33
+ expoPushToken: subscription.token,
34
+ user: userId || undefined,
35
+ });
36
+ } else {
37
+ const existingSub = await PushSubscription.findOne({ endpoint: subscription.endpoint });
38
+ if (existingSub) {
39
+ if (userId && String(existingSub.user) !== String(userId)) {
40
+ existingSub.user = userId;
41
+ await existingSub.save();
42
+ }
43
+ return SuccessResponse(res, 200, "Already subscribed.");
49
44
  }
50
45
 
51
- res.status(201).json({ success: true, message: "Subscription created." });
52
- } catch (error) {
53
- logger.error("Error in push subscribe controller", error);
54
- next(error);
46
+ await PushSubscription.create({
47
+ type: "WEB",
48
+ endpoint: subscription.endpoint,
49
+ keys: subscription.keys,
50
+ user: userId || undefined,
51
+ });
55
52
  }
56
- };
57
53
 
58
- export const unsubscribe = async (req, res, next) => {
59
- try {
60
- const { endpoint } = req.body;
61
- if (!endpoint) {
62
- return next({ status: 400, message: "Endpoint required" });
63
- }
54
+ SuccessResponse(res, 201, "Subscription created.");
55
+ });
64
56
 
65
- await PushSubscription.deleteOne({ endpoint });
66
-
67
- res.status(200).json({ success: true, message: "Unsubscribed." });
68
- } catch (error) {
69
- logger.error("Error in push unsubscribe controller", error);
70
- next(error);
57
+ export const unsubscribe = asyncHandler(async (req, res) => {
58
+ const { endpoint } = req.body;
59
+ if (!endpoint) {
60
+ throw new ApiError(400, "Endpoint required");
71
61
  }
72
- };
62
+
63
+ await PushSubscription.deleteOne({ endpoint });
64
+ SuccessResponse(res, 200, "Unsubscribed.");
65
+ });
@@ -1,26 +1,19 @@
1
1
  import { uploadSingleToCloudinary } from "../utils/cloudinaryUpload.js";
2
- import logger from "../utils/logger.js";
2
+ import { asyncHandler } from "../utils/asyncHandler.js";
3
+ import { SuccessResponse } from "../utils/SuccessResponse.js";
4
+ import { ApiError } from "../utils/ApiError.js";
3
5
 
4
6
  /**
5
7
  * Handle single file uploads to Cloudinary
6
8
  */
7
- export const uploadFile = async (req, res, next) => {
8
- try {
9
- if (!req.file) {
10
- return res.status(400).json({ success: false, message: "No file uploaded" });
11
- }
9
+ export const uploadFile = asyncHandler(async (req, res) => {
10
+ if (!req.file) {
11
+ throw new ApiError(400, "No file uploaded");
12
+ }
12
13
 
13
- const result = await uploadSingleToCloudinary(req.file, {
14
- folder: req.body.folder || "general_uploads",
15
- });
14
+ const result = await uploadSingleToCloudinary(req.file, {
15
+ folder: req.body.folder || "general_uploads",
16
+ });
16
17
 
17
- res.status(200).json({
18
- success: true,
19
- message: "File uploaded successfully",
20
- data: result,
21
- });
22
- } catch (error) {
23
- logger.error("Upload controller error", error);
24
- next(error);
25
- }
26
- };
18
+ SuccessResponse(res, 200, "File uploaded successfully", result);
19
+ });
@@ -1,7 +1,7 @@
1
- import jwt from "jsonwebtoken";
2
1
  import User from "../models/user.model.js";
3
2
  import { verifyToken } from "../utils/jwt.js";
4
3
  import logger from "../utils/logger.js";
4
+ import { ApiError } from "../utils/ApiError.js";
5
5
 
6
6
  export const protect = async (req, res, next) => {
7
7
  let token;
@@ -15,29 +15,23 @@ export const protect = async (req, res, next) => {
15
15
  }
16
16
 
17
17
  if (!token) {
18
- return res
19
- .status(401)
20
- .json({ message: "Not authorized, no token provided" });
18
+ return next(new ApiError(401, "Not authorized, no token provided"));
21
19
  }
22
20
 
23
21
  try {
24
22
  const decoded = verifyToken(token);
25
23
  if (!decoded) {
26
- return res.status(401).json({ message: "Not authorized, invalid token" });
24
+ return next(new ApiError(401, "Not authorized, invalid token"));
27
25
  }
28
26
 
29
27
  req.user = await User.findById(decoded.id).select("-password");
30
28
  if (!req.user) {
31
- return res
32
- .status(401)
33
- .json({ message: "Not authorized, user not found" });
29
+ return next(new ApiError(401, "Not authorized, user not found"));
34
30
  }
35
31
 
36
32
  return next();
37
33
  } catch (error) {
38
34
  logger.error("Auth middleware error", error);
39
- return res
40
- .status(401)
41
- .json({ message: "Not authorized, token verification failed" });
35
+ return next(new ApiError(401, "Not authorized, token verification failed"));
42
36
  }
43
37
  };
@@ -1,11 +1,11 @@
1
+ import { ApiError } from "../utils/ApiError.js";
2
+
1
3
  /**
2
4
  * Not Found Middleware
3
5
  * Catches all unmatched requests and forwards a 404 error
4
6
  */
5
7
  export const notFound = (req, res, next) => {
6
- const error = new Error(`Not Found - ${req.originalUrl}`);
7
- res.status(404);
8
- next(error);
8
+ next(new ApiError(404, `Not Found - ${req.originalUrl}`));
9
9
  };
10
10
 
11
11
  /**
@@ -13,10 +13,12 @@ export const notFound = (req, res, next) => {
13
13
  * Formats error responses nicely in JSON
14
14
  */
15
15
  export const errorHandler = (err, req, res, next) => {
16
- const statusCode = err.status || err.statusCode || (res.statusCode === 200 ? 500 : res.statusCode);
16
+ const statusCode = err.statusCode || err.status || (res.statusCode === 200 ? 500 : res.statusCode);
17
17
  res.status(statusCode).json({
18
18
  success: false,
19
+ statusCode,
19
20
  message: err.message || "An unexpected error occurred",
21
+ errors: err.errors || [],
20
22
  stack: process.env.NODE_ENV === "production" ? undefined : err.stack,
21
23
  });
22
24
  };
@@ -7,6 +7,7 @@ import {
7
7
  updateMe,
8
8
  genSignupOtp,
9
9
  savePushToken,
10
+ googleLogin,
10
11
  } from "../controllers/auth.controller.js";
11
12
  import { protect } from "../middleware/auth.middleware.js";
12
13
  import { apiLimiter } from "../middleware/rateLimiter.js";
@@ -18,6 +19,7 @@ router.use(apiLimiter);
18
19
  router.post("/send-signup-otp", genSignupOtp);
19
20
  router.post("/signup", signup);
20
21
  router.post("/login", login);
22
+ router.post("/google", googleLogin);
21
23
  router.post("/logout", logout);
22
24
  router.get("/me", protect, getMe);
23
25
  router.put("/me", protect, updateMe);