@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.
- package/README.md +2 -0
- package/package.json +1 -1
- package/src/commands/create.js +2 -0
- package/src/index.js +1 -1
- package/templates/layers/next/javascript/package.json +8 -2
- package/templates/layers/next/typescript/.eslintrc.json +12 -0
- package/templates/layers/next/typescript/package.json +14 -3
- package/templates/layers/next/typescript/src/components/common/Loading.tsx +1 -1
- package/templates/layers/next/typescript/src/components/common/LottieIcon.tsx +2 -2
- package/templates/layers/next/typescript/src/components/common/Table.tsx +4 -4
- package/templates/layers/next/typescript/src/components/modals/ConfirmModal.tsx +2 -1
- package/templates/layers/next/typescript/src/components/ui/Header.tsx +2 -2
- package/templates/layers/next/typescript/src/hooks/usePwaInstall.ts +10 -4
- package/templates/layers/next/typescript/src/services/authService.ts +1 -1
- package/templates/layers/next/typescript/src/store/useAuthStore.ts +24 -9
- package/templates/layers/node/javascript/.env.example +3 -1
- package/templates/layers/node/javascript/package.json +12 -3
- package/templates/layers/node/javascript/src/app.js +2 -1
- package/templates/layers/node/javascript/src/controllers/auth.controller.js +106 -168
- package/templates/layers/node/javascript/src/controllers/push.controller.js +51 -58
- package/templates/layers/node/javascript/src/controllers/upload.controller.js +12 -19
- package/templates/layers/node/javascript/src/middleware/auth.middleware.js +5 -11
- package/templates/layers/node/javascript/src/middleware/errorHandler.js +6 -4
- package/templates/layers/node/javascript/src/routes/auth.route.js +2 -0
- package/templates/layers/node/javascript/src/services/auth.service.js +159 -164
- package/templates/layers/node/javascript/src/utils/ApiError.js +20 -0
- package/templates/layers/node/javascript/src/utils/SuccessResponse.js +15 -0
- package/templates/layers/node/javascript/src/utils/apiResponse.js +4 -0
- package/templates/layers/node/javascript/src/utils/asyncHandler.js +5 -0
- package/templates/layers/node/typescript/.env.example +3 -1
- package/templates/layers/node/typescript/.eslintrc.cjs +28 -0
- package/templates/layers/node/typescript/package.json +16 -2
- package/templates/layers/node/typescript/src/app.ts +3 -2
- package/templates/layers/node/typescript/src/config/db.ts +3 -2
- package/templates/layers/node/typescript/src/config/socket.ts +5 -4
- package/templates/layers/node/typescript/src/controllers/auth.controller.ts +104 -132
- package/templates/layers/node/typescript/src/controllers/push.controller.ts +52 -59
- package/templates/layers/node/typescript/src/controllers/upload.controller.ts +12 -19
- package/templates/layers/node/typescript/src/middleware/auth.middleware.ts +13 -7
- package/templates/layers/node/typescript/src/middleware/errorHandler.ts +18 -5
- package/templates/layers/node/typescript/src/middleware/rateLimiter.ts +2 -2
- package/templates/layers/node/typescript/src/routes/auth.route.ts +2 -0
- package/templates/layers/node/typescript/src/services/auth.service.ts +86 -43
- package/templates/layers/node/typescript/src/types/express.d.ts +5 -3
- package/templates/layers/node/typescript/src/utils/ApiError.ts +25 -0
- package/templates/layers/node/typescript/src/utils/SuccessResponse.ts +17 -0
- package/templates/layers/node/typescript/src/utils/apiResponse.ts +4 -0
- package/templates/layers/node/typescript/src/utils/asyncHandler.ts +13 -0
- package/templates/layers/node/typescript/src/utils/cloudinaryUpload.ts +18 -7
- package/templates/layers/node/typescript/src/utils/expoNotification.ts +1 -1
- package/templates/layers/node/typescript/src/utils/genOtpToken.ts +7 -1
- package/templates/layers/node/typescript/src/utils/jwt.ts +8 -2
- package/templates/layers/node/typescript/src/utils/logger.ts +2 -1
- package/templates/layers/node/typescript/src/utils/pushHelper.ts +13 -9
- package/templates/layers/node-prisma/javascript/.env.example +3 -1
- package/templates/layers/node-prisma/javascript/package.json +11 -2
- package/templates/layers/node-prisma/javascript/src/app.js +2 -1
- package/templates/layers/node-prisma/javascript/src/controllers/auth.controller.js +90 -135
- package/templates/layers/node-prisma/javascript/src/controllers/push.controller.js +64 -76
- package/templates/layers/node-prisma/javascript/src/controllers/upload.controller.js +12 -19
- package/templates/layers/node-prisma/javascript/src/middleware/auth.middleware.js +5 -4
- package/templates/layers/node-prisma/javascript/src/middleware/errorHandler.js +6 -4
- package/templates/layers/node-prisma/javascript/src/routes/auth.route.js +2 -0
- package/templates/layers/node-prisma/javascript/src/services/auth.service.js +145 -122
- package/templates/layers/node-prisma/javascript/src/utils/ApiError.js +20 -0
- package/templates/layers/node-prisma/javascript/src/utils/SuccessResponse.js +15 -0
- package/templates/layers/node-prisma/javascript/src/utils/apiResponse.js +4 -0
- package/templates/layers/node-prisma/javascript/src/utils/asyncHandler.js +5 -0
- package/templates/layers/node-prisma/typescript/.env.example +3 -1
- package/templates/layers/node-prisma/typescript/.eslintrc.cjs +28 -0
- package/templates/layers/node-prisma/typescript/package.json +19 -5
- package/templates/layers/node-prisma/typescript/src/app.ts +3 -2
- package/templates/layers/node-prisma/typescript/src/config/prisma.ts +3 -2
- package/templates/layers/node-prisma/typescript/src/config/socket.ts +5 -4
- package/templates/layers/node-prisma/typescript/src/controllers/auth.controller.ts +89 -105
- package/templates/layers/node-prisma/typescript/src/controllers/push.controller.ts +67 -83
- package/templates/layers/node-prisma/typescript/src/controllers/upload.controller.ts +12 -19
- package/templates/layers/node-prisma/typescript/src/middleware/auth.middleware.ts +9 -13
- package/templates/layers/node-prisma/typescript/src/middleware/errorHandler.ts +18 -5
- package/templates/layers/node-prisma/typescript/src/middleware/rateLimiter.ts +2 -2
- package/templates/layers/node-prisma/typescript/src/routes/auth.route.ts +2 -0
- package/templates/layers/node-prisma/typescript/src/services/auth.service.ts +83 -31
- package/templates/layers/node-prisma/typescript/src/types/express.d.ts +5 -3
- package/templates/layers/node-prisma/typescript/src/utils/ApiError.ts +25 -0
- package/templates/layers/node-prisma/typescript/src/utils/SuccessResponse.ts +17 -0
- package/templates/layers/node-prisma/typescript/src/utils/apiResponse.ts +4 -0
- package/templates/layers/node-prisma/typescript/src/utils/asyncHandler.ts +13 -0
- package/templates/layers/node-prisma/typescript/src/utils/cloudinaryUpload.ts +18 -7
- package/templates/layers/node-prisma/typescript/src/utils/genOtpToken.ts +7 -1
- package/templates/layers/node-prisma/typescript/src/utils/jwt.ts +8 -2
- package/templates/layers/node-prisma/typescript/src/utils/logger.ts +2 -1
- package/templates/layers/node-prisma/typescript/src/utils/pushHelper.ts +10 -6
- package/templates/layers/prisma/javascript/package.json +8 -0
- package/templates/layers/prisma/typescript/package.json +14 -1
- package/templates/layers/prisma/typescript/src/config/db.ts +3 -2
- package/templates/layers/react/javascript/package.json +6 -0
- package/templates/layers/react/typescript/.eslintrc.cjs +20 -0
- package/templates/layers/react/typescript/package.json +11 -2
- package/templates/layers/react/typescript/src/components/common/Table.tsx +4 -4
- package/templates/layers/react/typescript/src/components/ui/Sidebar.tsx +1 -1
- package/templates/layers/react/typescript/src/main.tsx +11 -11
- package/templates/layers/react/typescript/src/services/authService.ts +1 -1
- package/templates/layers/react/typescript/src/store/useAuthStore.ts +24 -9
- package/templates/layers/react-native/base/app.json +9 -22
- package/templates/layers/react-native/base/eslint.config.js +5 -1
- package/templates/layers/react-native/base/package.json +40 -33
- package/templates/layers/react-native/base/src/app/(auth)/edit-profile.tsx +7 -7
- package/templates/layers/react-native/base/src/app/(auth)/login.tsx +31 -7
- package/templates/layers/react-native/base/src/app/(tabs)/_layout.tsx +3 -3
- package/templates/layers/react-native/base/src/app/(tabs)/index.tsx +6 -5
- package/templates/layers/react-native/base/src/app/_layout.tsx +4 -8
- package/templates/layers/react-native/base/src/app/screens/doc-viewer.tsx +378 -0
- package/templates/layers/react-native/base/src/components/common/ConfirmationToast.tsx +58 -40
- package/templates/layers/react-native/base/src/components/common/GoogleAuthButton.tsx +123 -0
- package/templates/layers/react-native/base/src/components/common/LocationLoader.tsx +64 -9
- package/templates/layers/react-native/base/src/components/common/Typography.tsx +54 -19
- package/templates/layers/react-native/base/src/components/ui/BiometricLock.tsx +3 -3
- package/templates/layers/react-native/base/src/components/ui/ClassicTabBar.tsx +15 -12
- package/templates/layers/react-native/base/src/components/ui/CustomTabBar.tsx +90 -91
- package/templates/layers/react-native/base/src/components/ui/Drawer.tsx +344 -0
- package/templates/layers/react-native/base/src/components/ui/Sidebar.tsx +4 -4
- package/templates/layers/react-native/base/src/components/ui/buttons/Button.tsx +29 -6
- package/templates/layers/react-native/base/src/components/ui/data-display/Accordion.tsx +2 -2
- package/templates/layers/react-native/base/src/components/ui/data-display/Badge.tsx +1 -2
- package/templates/layers/react-native/base/src/components/ui/data-display/Image.tsx +3 -3
- package/templates/layers/react-native/base/src/components/ui/data-display/SwipeableRow.tsx +28 -7
- package/templates/layers/react-native/base/src/components/ui/feedback/GestureWrapper.tsx +11 -4
- package/templates/layers/react-native/base/src/components/ui/inputs/DatePicker.tsx +3 -3
- package/templates/layers/react-native/base/src/components/ui/inputs/ImagePickerModal.tsx +45 -19
- package/templates/layers/react-native/base/src/components/ui/inputs/Input.tsx +28 -9
- package/templates/layers/react-native/base/src/components/ui/inputs/SearchablePicker.tsx +199 -0
- package/templates/layers/react-native/base/src/components/ui/inputs/Switch.tsx +7 -7
- package/templates/layers/react-native/base/src/components/ui/inputs/WebLocationPicker.tsx +146 -72
- package/templates/layers/react-native/base/src/config/api.ts +5 -5
- package/templates/layers/react-native/base/src/services/auth.service.ts +3 -1
- package/templates/layers/react-native/base/src/services/permission.service.ts +168 -44
- package/templates/layers/react-native/base/src/services/socket.service.ts +0 -7
- package/templates/layers/react-native/base/src/store/useAuthStore.ts +61 -16
- package/templates/layers/react-native/base/src/store/useNotificationStore.ts +36 -42
- package/templates/layers/react-native/base/src/types/images.d.ts +7 -5
- package/templates/layers/react-native/base/src/utils/env.ts +47 -0
- package/templates/layers/react-native/base/src/utils/fileDownload.ts +206 -0
- package/templates/layers/react-native/base/src/utils/storage.ts +1 -1
- package/templates/presets/production-fullstack/overrides/client/package.json +9 -0
- package/templates/presets/production-fullstack/overrides/mobile/app.json +2 -15
- package/templates/presets/production-fullstack/overrides/mobile/package.json +39 -29
- package/templates/presets/production-fullstack/overrides/mobile/src/app/_layout.tsx +3 -3
- package/templates/presets/production-fullstack/overrides/mobile/src/components/ui/Drawer.tsx +344 -0
- package/templates/presets/production-fullstack/overrides/mobile/src/components/ui/inputs/DatePicker.tsx +3 -3
- package/templates/presets/production-fullstack/overrides/mobile/src/components/ui/inputs/ImagePickerModal.tsx +3 -3
- package/templates/presets/production-fullstack/overrides/mobile/src/services/permission.service.ts +168 -44
- package/templates/presets/production-fullstack/overrides/mobile/src/store/useNotificationStore.ts +16 -0
- package/templates/presets/production-fullstack/overrides/mobile/src/utils/env.ts +47 -0
- package/templates/presets/production-fullstack/overrides/server/package.json +11 -2
- package/templates/layers/react-native/base/src/app/doc-viewer.tsx +0 -193
- package/templates/layers/react-native/base/src/components/ui/BottomModal.tsx +0 -176
- package/templates/presets/production-fullstack/overrides/mobile/src/components/ui/BottomModal.tsx +0 -159
- /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
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
153
|
-
|
|
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
|
|
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
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
14
|
-
|
|
10
|
+
if (!subscription) {
|
|
11
|
+
throw new ApiError(400, "Invalid subscription payload");
|
|
12
|
+
}
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
14
|
+
const isExpo = !!subscription.token;
|
|
15
|
+
const isWeb = !!(subscription.endpoint && subscription.keys);
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
59
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const result = await uploadSingleToCloudinary(req.file, {
|
|
15
|
+
folder: req.body.folder || "general_uploads",
|
|
16
|
+
});
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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.
|
|
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);
|