exp-node-server 1.1.7 → 2.0.0
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 +82 -20
- package/dist/cli.js +525 -0
- package/package.json +29 -9
- package/templates/mongoose/.dockerignore +10 -0
- package/templates/mongoose/.env.example +56 -7
- package/templates/mongoose/.prettierignore +3 -0
- package/templates/mongoose/.prettierrc +7 -0
- package/templates/mongoose/Dockerfile +33 -0
- package/templates/mongoose/_gitignore +32 -0
- package/templates/mongoose/docker-compose.yml +21 -0
- package/templates/mongoose/eslint.config.js +35 -0
- package/templates/mongoose/package-lock.json +6377 -0
- package/templates/mongoose/package.json +49 -10
- package/templates/mongoose/readme.md +85 -0
- package/templates/mongoose/src/app/configs/index.ts +178 -26
- package/templates/mongoose/src/app/errors/zodError.ts +16 -16
- package/templates/mongoose/src/app/lib/db.ts +18 -0
- package/templates/mongoose/src/app/lib/logger.ts +12 -0
- package/templates/mongoose/src/app/middlewares/auth.ts +48 -44
- package/templates/mongoose/src/app/middlewares/global_error_handler.ts +59 -65
- package/templates/mongoose/src/app/middlewares/not_found_api.ts +7 -7
- package/templates/mongoose/src/app/middlewares/rate_limiter.ts +153 -0
- package/templates/mongoose/src/app/middlewares/request_validator.ts +11 -10
- package/templates/mongoose/src/app/middlewares/uploader.ts +17 -13
- package/templates/mongoose/src/app/modules/account/account.controller.ts +156 -0
- package/templates/mongoose/src/app/modules/account/account.interface.ts +65 -0
- package/templates/mongoose/src/app/modules/account/account.route.ts +101 -0
- package/templates/mongoose/src/app/modules/account/account.schema.ts +97 -0
- package/templates/mongoose/src/app/modules/account/account.service.ts +422 -0
- package/templates/mongoose/src/app/modules/account/account.swagger.ts +278 -0
- package/templates/mongoose/src/app/modules/account/account.validation.ts +63 -0
- package/templates/mongoose/src/app/modules/profile/profile.controller.ts +17 -0
- package/templates/mongoose/src/app/modules/profile/profile.interface.ts +7 -0
- package/templates/mongoose/src/app/modules/profile/profile.route.ts +28 -0
- package/templates/mongoose/src/app/modules/profile/profile.schema.ts +13 -0
- package/templates/mongoose/src/app/modules/profile/profile.service.ts +28 -0
- package/templates/mongoose/src/app/modules/profile/profile.swagger.ts +34 -0
- package/templates/mongoose/src/app/modules/profile/profile.validation.ts +8 -0
- package/templates/mongoose/src/app/passport-js/index.ts +87 -0
- package/templates/mongoose/src/app/passport-js/oauth-state.ts +150 -0
- package/templates/mongoose/src/app/passport-js/providers.ts +258 -0
- package/templates/mongoose/src/app/passport-js/social-account.ts +141 -0
- package/templates/mongoose/src/app/queues/agenda.ts +24 -0
- package/templates/mongoose/src/app/queues/email.queue.ts +53 -0
- package/templates/mongoose/src/app/queues/index.ts +13 -0
- package/templates/mongoose/src/app/templates/otpEmailTemplate.ts +185 -0
- package/templates/mongoose/src/app/templates/resetPasswordEmailTemplate.ts +157 -0
- package/templates/mongoose/src/app/types/error.d.ts +7 -7
- package/templates/mongoose/src/app/types/express.d.ts +11 -0
- package/templates/mongoose/src/app/types/passport-oauth1.d.ts +51 -0
- package/templates/mongoose/src/app/utils/JWT.ts +38 -18
- package/templates/mongoose/src/app/utils/app_error.ts +10 -10
- package/templates/mongoose/src/app/utils/auth_cookie.ts +27 -0
- package/templates/mongoose/src/app/utils/catch_async.ts +8 -8
- package/templates/mongoose/src/app/utils/cloudinary.ts +26 -59
- package/templates/mongoose/src/app/utils/mail_sender.ts +26 -92
- package/templates/mongoose/src/app/utils/manage_response.ts +19 -19
- package/templates/mongoose/src/app/utils/otp.ts +20 -0
- package/templates/mongoose/src/app/utils/session.ts +55 -0
- package/templates/mongoose/src/app.ts +73 -42
- package/templates/mongoose/src/routes.ts +13 -13
- package/templates/mongoose/src/server.ts +38 -18
- package/templates/mongoose/src/swaggerOptions.ts +23 -23
- package/templates/mongoose/tests/app.test.ts +97 -0
- package/templates/mongoose/tests/global-setup.ts +15 -0
- package/templates/mongoose/tests/helpers/fixtures.ts +42 -0
- package/templates/mongoose/tests/routes/account.test.ts +614 -0
- package/templates/mongoose/tests/routes/profile.test.ts +95 -0
- package/templates/mongoose/tests/routes/social.test.ts +310 -0
- package/templates/mongoose/tests/setup.ts +25 -0
- package/templates/mongoose/tsconfig.json +16 -20
- package/templates/mongoose/vitest.config.ts +43 -0
- package/templates/prisma/.dockerignore +2 -0
- package/templates/prisma/.env.example +62 -6
- package/templates/prisma/.prettierignore +3 -0
- package/templates/prisma/.prettierrc +7 -0
- package/templates/prisma/.projectrc.json +1 -1
- package/templates/prisma/Dockerfile +21 -7
- package/templates/prisma/_gitignore +35 -0
- package/templates/prisma/docker-compose.yml +8 -7
- package/templates/prisma/eslint.config.js +35 -0
- package/templates/prisma/package-lock.json +7327 -0
- package/templates/prisma/package.json +47 -9
- package/templates/prisma/prisma/schema/account.schema.prisma +92 -10
- package/templates/prisma/prisma/schema/profile.schema.prisma +8 -3
- package/templates/prisma/prisma/schema/schema.prisma +1 -2
- package/templates/prisma/prisma.config.ts +5 -5
- package/templates/prisma/readme.md +81 -0
- package/templates/prisma/src/app/configs/index.ts +175 -26
- package/templates/prisma/src/app/errors/zodError.ts +16 -16
- package/templates/prisma/src/app/lib/logger.ts +12 -0
- package/templates/prisma/src/app/lib/prisma.ts +4 -6
- package/templates/prisma/src/app/middlewares/auth.ts +37 -16
- package/templates/prisma/src/app/middlewares/global_error_handler.ts +42 -35
- package/templates/prisma/src/app/middlewares/not_found_api.ts +7 -7
- package/templates/prisma/src/app/middlewares/rate_limiter.ts +124 -0
- package/templates/prisma/src/app/middlewares/request_validator.ts +4 -3
- package/templates/prisma/src/app/middlewares/uploader.ts +17 -13
- package/templates/prisma/src/app/modules/account/account.controller.ts +74 -34
- package/templates/prisma/src/app/modules/account/account.route.ts +68 -22
- package/templates/prisma/src/app/modules/account/account.service.ts +329 -309
- package/templates/prisma/src/app/modules/account/account.swagger.ts +167 -76
- package/templates/prisma/src/app/modules/account/account.validation.ts +41 -19
- package/templates/prisma/src/app/modules/profile/profile.controller.ts +4 -4
- package/templates/prisma/src/app/modules/profile/profile.route.ts +18 -12
- package/templates/prisma/src/app/modules/profile/profile.service.ts +11 -18
- package/templates/prisma/src/app/modules/profile/profile.swagger.ts +11 -11
- package/templates/prisma/src/app/modules/profile/profile.validation.ts +2 -2
- package/templates/prisma/src/app/passport-js/index.ts +87 -0
- package/templates/prisma/src/app/passport-js/oauth-state.ts +150 -0
- package/templates/prisma/src/app/passport-js/providers.ts +263 -0
- package/templates/prisma/src/app/passport-js/social-account.ts +147 -0
- package/templates/prisma/src/app/queues/boss.ts +8 -0
- package/templates/prisma/src/app/queues/email.queue.ts +56 -0
- package/templates/prisma/src/app/queues/index.ts +14 -0
- package/templates/prisma/src/app/queues/maintenance.queue.ts +30 -0
- package/templates/prisma/src/app/templates/otpEmailTemplate.ts +185 -0
- package/templates/prisma/src/app/templates/resetPasswordEmailTemplate.ts +157 -0
- package/templates/prisma/src/app/types/error.d.ts +7 -7
- package/templates/prisma/src/app/types/express.d.ts +11 -0
- package/templates/prisma/src/app/types/passport-oauth1.d.ts +51 -0
- package/templates/prisma/src/app/utils/JWT.ts +36 -17
- package/templates/prisma/src/app/utils/app_error.ts +10 -10
- package/templates/prisma/src/app/utils/auth_cookie.ts +27 -0
- package/templates/prisma/src/app/utils/catch_async.ts +8 -8
- package/templates/prisma/src/app/utils/cloudinary.ts +26 -59
- package/templates/prisma/src/app/utils/mail_sender.ts +26 -92
- package/templates/prisma/src/app/utils/manage_response.ts +19 -19
- package/templates/prisma/src/app/utils/otp.ts +20 -0
- package/templates/prisma/src/app/utils/session.ts +57 -0
- package/templates/prisma/src/app.ts +73 -42
- package/templates/prisma/src/routes.ts +12 -10
- package/templates/prisma/src/server.ts +37 -19
- package/templates/prisma/src/swaggerOptions.ts +22 -26
- package/templates/prisma/tests/app.test.ts +86 -0
- package/templates/prisma/tests/helpers/fixtures.ts +35 -0
- package/templates/prisma/tests/helpers/prisma.ts +32 -0
- package/templates/prisma/tests/routes/account.test.ts +627 -0
- package/templates/prisma/tests/routes/profile.test.ts +91 -0
- package/templates/prisma/tests/routes/social.test.ts +317 -0
- package/templates/prisma/tests/setup.ts +10 -0
- package/templates/prisma/tsconfig.json +16 -23
- package/templates/prisma/vitest.config.ts +38 -0
- package/bin/cli.js +0 -41
- package/src/createProject.js +0 -61
- package/src/generateModule.js +0 -93
- package/src/utils/getProjectType.js +0 -13
- package/src/utils/mongoose/controllerGenerator.js +0 -70
- package/src/utils/mongoose/routeGenerator.js +0 -27
- package/src/utils/mongoose/schemaGenerator.js +0 -11
- package/src/utils/mongoose/serviceGenerator.js +0 -49
- package/src/utils/mongoose/swaggerGenerator.js +0 -112
- package/src/utils/mongoose/validationGenerator.js +0 -13
- package/src/utils/prisma/prismaSchemaGenerator.js +0 -10
- package/src/utils/prisma/prismaServiceGenerator.js +0 -47
- package/src/utils/routeInjector.js +0 -40
- package/src/utils/swaggerInjector.js +0 -52
- package/src/utils/validateModuleName.js +0 -17
- package/src/utils/validateProjectName.js +0 -15
- package/src/utils.js +0 -26
- package/templates/mongoose/src/app/errors/castError.ts +0 -23
- package/templates/mongoose/src/app/errors/duplicateError.ts +0 -26
- package/templates/mongoose/src/app/errors/validationError.ts +0 -25
- package/templates/mongoose/src/app/modules/auth/auth.controller.ts +0 -127
- package/templates/mongoose/src/app/modules/auth/auth.interface.ts +0 -24
- package/templates/mongoose/src/app/modules/auth/auth.route.ts +0 -46
- package/templates/mongoose/src/app/modules/auth/auth.schema.ts +0 -19
- package/templates/mongoose/src/app/modules/auth/auth.service.ts +0 -324
- package/templates/mongoose/src/app/modules/auth/auth.swagger.ts +0 -214
- package/templates/mongoose/src/app/modules/auth/auth.validation.ts +0 -37
- package/templates/mongoose/src/app/modules/user/user.controller.ts +0 -17
- package/templates/mongoose/src/app/modules/user/user.interface.ts +0 -15
- package/templates/mongoose/src/app/modules/user/user.route.ts +0 -20
- package/templates/mongoose/src/app/modules/user/user.schema.ts +0 -22
- package/templates/mongoose/src/app/modules/user/user.service.ts +0 -22
- package/templates/mongoose/src/app/modules/user/user.swagger.ts +0 -75
- package/templates/mongoose/src/app/modules/user/user.validation.ts +0 -18
- package/templates/mongoose/src/app/types/index.d.ts +0 -12
- package/templates/mongoose/src/app/utils/isAccountExist.ts +0 -31
- package/templates/prisma/src/app/types/index.d.ts +0 -12
- package/templates/prisma/src/app/utils/otpGenerator.ts +0 -29
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { configs } from "../../configs";
|
|
2
|
-
import catchAsync from "../../utils/catch_async";
|
|
3
|
-
import manageResponse from "../../utils/manage_response";
|
|
4
|
-
import { auth_services } from "./auth.service";
|
|
5
|
-
|
|
6
|
-
const register_user = catchAsync(async (req, res) => {
|
|
7
|
-
const result = await auth_services.register_user_into_db(req?.body);
|
|
8
|
-
manageResponse(res, {
|
|
9
|
-
success: true,
|
|
10
|
-
message: "Account created successful",
|
|
11
|
-
statusCode: 200,
|
|
12
|
-
data: result,
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
const login_user = catchAsync(async (req, res) => {
|
|
17
|
-
const result = await auth_services.login_user_from_db(req.body);
|
|
18
|
-
|
|
19
|
-
res.cookie("refreshToken", result.refreshToken, {
|
|
20
|
-
secure: configs.env == "production",
|
|
21
|
-
httpOnly: true,
|
|
22
|
-
});
|
|
23
|
-
manageResponse(res, {
|
|
24
|
-
statusCode: 200,
|
|
25
|
-
success: true,
|
|
26
|
-
message: "User is logged in successful !",
|
|
27
|
-
data: {
|
|
28
|
-
accessToken: result.accessToken,
|
|
29
|
-
role: result?.role,
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const get_my_profile = catchAsync(async (req, res) => {
|
|
35
|
-
const { email } = req.user!;
|
|
36
|
-
const result = await auth_services.get_my_profile_from_db(email);
|
|
37
|
-
manageResponse(res, {
|
|
38
|
-
statusCode: 200,
|
|
39
|
-
success: true,
|
|
40
|
-
message: "User profile fetched successfully!",
|
|
41
|
-
data: result,
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const refresh_token = catchAsync(async (req, res) => {
|
|
46
|
-
const { refreshToken } = req.cookies;
|
|
47
|
-
const result = await auth_services.refresh_token_from_db(refreshToken);
|
|
48
|
-
manageResponse(res, {
|
|
49
|
-
statusCode: 200,
|
|
50
|
-
success: true,
|
|
51
|
-
message: "Refresh token generated successfully!",
|
|
52
|
-
data: result,
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const change_password = catchAsync(async (req, res) => {
|
|
57
|
-
const user = req?.user;
|
|
58
|
-
const result = await auth_services.change_password_from_db(user!, req.body);
|
|
59
|
-
|
|
60
|
-
manageResponse(res, {
|
|
61
|
-
statusCode: 200,
|
|
62
|
-
success: true,
|
|
63
|
-
message: "Password changed successfully!",
|
|
64
|
-
data: result,
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const forget_password = catchAsync(async (req, res) => {
|
|
69
|
-
const { email } = req?.body;
|
|
70
|
-
await auth_services.forget_password_from_db(email);
|
|
71
|
-
manageResponse(res, {
|
|
72
|
-
statusCode: 200,
|
|
73
|
-
success: true,
|
|
74
|
-
message: "Reset password link sent to your email!",
|
|
75
|
-
data: null,
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
const reset_password = catchAsync(async (req, res) => {
|
|
80
|
-
const { token, newPassword, email } = req.body;
|
|
81
|
-
const result = await auth_services.reset_password_into_db(
|
|
82
|
-
token,
|
|
83
|
-
email,
|
|
84
|
-
newPassword,
|
|
85
|
-
);
|
|
86
|
-
manageResponse(res, {
|
|
87
|
-
statusCode: 200,
|
|
88
|
-
success: true,
|
|
89
|
-
message: "Password reset successfully!",
|
|
90
|
-
data: result,
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const verified_account = catchAsync(async (req, res) => {
|
|
95
|
-
const result = await auth_services.verified_account_into_db(req?.body?.token);
|
|
96
|
-
|
|
97
|
-
manageResponse(res, {
|
|
98
|
-
statusCode: 200,
|
|
99
|
-
success: true,
|
|
100
|
-
message: "Account Verification successful.",
|
|
101
|
-
data: result,
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
const get_new_verification_link = catchAsync(async (req, res) => {
|
|
106
|
-
const result = await auth_services.get_new_verification_link_from_db(
|
|
107
|
-
req?.body?.email,
|
|
108
|
-
);
|
|
109
|
-
manageResponse(res, {
|
|
110
|
-
statusCode: 200,
|
|
111
|
-
success: true,
|
|
112
|
-
message: "New Verification link is send on email.",
|
|
113
|
-
data: result,
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
export const auth_controllers = {
|
|
118
|
-
register_user,
|
|
119
|
-
login_user,
|
|
120
|
-
get_my_profile,
|
|
121
|
-
refresh_token,
|
|
122
|
-
change_password,
|
|
123
|
-
reset_password,
|
|
124
|
-
forget_password,
|
|
125
|
-
verified_account,
|
|
126
|
-
get_new_verification_link,
|
|
127
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export type TAccount = {
|
|
2
|
-
email: string;
|
|
3
|
-
password: string;
|
|
4
|
-
lastPasswordChange?: Date;
|
|
5
|
-
isDeleted?: boolean;
|
|
6
|
-
accountStatus?: "ACTIVE" | "INACTIVE" | "SUSPENDED";
|
|
7
|
-
role?: "USER" | "ADMIN",
|
|
8
|
-
isVerified?: boolean,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export interface TRegisterPayload extends TAccount {
|
|
13
|
-
name: string
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export type TLoginPayload = {
|
|
17
|
-
email: string;
|
|
18
|
-
password: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type TJwtUser = {
|
|
22
|
-
email: string,
|
|
23
|
-
role?: "USER" | "ADMIN",
|
|
24
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { Router } from "express";
|
|
2
|
-
import { auth_controllers } from "./auth.controller";
|
|
3
|
-
import RequestValidator from "../../middlewares/request_validator";
|
|
4
|
-
import { auth_validation } from "./auth.validation";
|
|
5
|
-
import auth from "../../middlewares/auth";
|
|
6
|
-
|
|
7
|
-
const authRoute = Router()
|
|
8
|
-
|
|
9
|
-
authRoute.post("/register", RequestValidator(auth_validation.register_validation), auth_controllers.register_user)
|
|
10
|
-
authRoute.post("/login", RequestValidator(auth_validation.login_validation), auth_controllers.login_user)
|
|
11
|
-
|
|
12
|
-
authRoute.get(
|
|
13
|
-
'/me',
|
|
14
|
-
auth("ADMIN", "USER"),
|
|
15
|
-
auth_controllers.get_my_profile,
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
authRoute.post('/refresh-token', auth_controllers.refresh_token);
|
|
19
|
-
authRoute.post(
|
|
20
|
-
'/change-password',
|
|
21
|
-
auth("ADMIN", "USER"),
|
|
22
|
-
RequestValidator(auth_validation.changePassword),
|
|
23
|
-
auth_controllers.change_password,
|
|
24
|
-
);
|
|
25
|
-
authRoute.post(
|
|
26
|
-
'/forgot-password',
|
|
27
|
-
RequestValidator(auth_validation.forgotPassword),
|
|
28
|
-
auth_controllers.forget_password,
|
|
29
|
-
);
|
|
30
|
-
authRoute.post(
|
|
31
|
-
'/reset-password',
|
|
32
|
-
RequestValidator(auth_validation.resetPassword),
|
|
33
|
-
auth_controllers.reset_password,
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
authRoute.post(
|
|
37
|
-
"/verified-account",
|
|
38
|
-
RequestValidator(auth_validation.verified_account),
|
|
39
|
-
auth_controllers.verified_account
|
|
40
|
-
)
|
|
41
|
-
authRoute.post(
|
|
42
|
-
"/new-verification-link",
|
|
43
|
-
RequestValidator(auth_validation.forgotPassword),
|
|
44
|
-
auth_controllers.get_new_verification_link
|
|
45
|
-
)
|
|
46
|
-
export default authRoute;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { model, Schema } from "mongoose";
|
|
2
|
-
import { TAccount } from "./auth.interface";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const authSchema = new Schema<TAccount>({
|
|
6
|
-
email: { type: String, required: true },
|
|
7
|
-
password: { type: String, required: true },
|
|
8
|
-
lastPasswordChange: { type: String },
|
|
9
|
-
isDeleted: { type: Boolean, default: false },
|
|
10
|
-
accountStatus: { type: String, default: "ACTIVE" },
|
|
11
|
-
role: { type: String, default: "USER" },
|
|
12
|
-
isVerified: { type: Boolean, default: false }
|
|
13
|
-
}, {
|
|
14
|
-
versionKey: false,
|
|
15
|
-
timestamps: true
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export const Account_Model = model("account", authSchema)
|
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
import bcrypt from "bcrypt";
|
|
2
|
-
import { JwtPayload, Secret } from "jsonwebtoken";
|
|
3
|
-
import mongoose from "mongoose";
|
|
4
|
-
import { configs } from "../../configs";
|
|
5
|
-
import { AppError } from "../../utils/app_error";
|
|
6
|
-
import { isAccountExist } from "../../utils/isAccountExist";
|
|
7
|
-
import { jwtHelpers } from "../../utils/JWT";
|
|
8
|
-
import sendMail from "../../utils/mail_sender";
|
|
9
|
-
import { TUser } from "../user/user.interface";
|
|
10
|
-
import { User_Model } from "../user/user.schema";
|
|
11
|
-
import { TAccount, TLoginPayload, TRegisterPayload } from "./auth.interface";
|
|
12
|
-
import { Account_Model } from "./auth.schema";
|
|
13
|
-
// register user
|
|
14
|
-
const register_user_into_db = async (payload: TRegisterPayload) => {
|
|
15
|
-
const session = await mongoose.startSession();
|
|
16
|
-
session.startTransaction();
|
|
17
|
-
try {
|
|
18
|
-
// Check if the account already exists
|
|
19
|
-
const isExistAccount = await Account_Model.findOne(
|
|
20
|
-
{ email: payload?.email },
|
|
21
|
-
null,
|
|
22
|
-
{ session },
|
|
23
|
-
);
|
|
24
|
-
if (isExistAccount) {
|
|
25
|
-
throw new AppError("Account already exist!!", 409);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Hash the password
|
|
29
|
-
const hashPassword = bcrypt.hashSync(payload?.password, 10);
|
|
30
|
-
|
|
31
|
-
// Create account
|
|
32
|
-
const accountPayload: TAccount = {
|
|
33
|
-
email: payload.email,
|
|
34
|
-
password: hashPassword,
|
|
35
|
-
lastPasswordChange: new Date(),
|
|
36
|
-
};
|
|
37
|
-
const newAccount = await Account_Model.create([accountPayload], {
|
|
38
|
-
session,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// Create user
|
|
42
|
-
const userPayload: TUser = {
|
|
43
|
-
name: payload.name,
|
|
44
|
-
accountId: newAccount[0]._id,
|
|
45
|
-
};
|
|
46
|
-
await User_Model.create([userPayload], { session });
|
|
47
|
-
// make verified link
|
|
48
|
-
const verifiedToken = jwtHelpers.generateToken(
|
|
49
|
-
{
|
|
50
|
-
email: payload?.email,
|
|
51
|
-
},
|
|
52
|
-
configs.jwt.verified_token as Secret,
|
|
53
|
-
"5m",
|
|
54
|
-
);
|
|
55
|
-
const verificationLink = `${configs.jwt.front_end_url}/verified?token=${verifiedToken}`;
|
|
56
|
-
// Commit the transaction
|
|
57
|
-
await session.commitTransaction();
|
|
58
|
-
await sendMail({
|
|
59
|
-
to: payload?.email,
|
|
60
|
-
subject: "Thanks for creating account!",
|
|
61
|
-
textBody: `New Account successfully created on ${new Date().toLocaleDateString()}`,
|
|
62
|
-
name: payload?.name,
|
|
63
|
-
htmlBody: `
|
|
64
|
-
<p>Thanks for creating an account with us. We’re excited to have you on board! Click the button below to
|
|
65
|
-
verify your email and activate your account:</p>
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
<div style="text-align: center; margin: 30px 0;">
|
|
69
|
-
<a href="${verificationLink}" target="_blank"
|
|
70
|
-
style="background-color: #4CAF50; color: #ffffff; padding: 14px 28px; text-decoration: none; font-weight: bold; border-radius: 5px; display: inline-block; font-size: 18px;"
|
|
71
|
-
class="btn">
|
|
72
|
-
Verify My Email
|
|
73
|
-
</a>
|
|
74
|
-
</div>
|
|
75
|
-
|
|
76
|
-
<p>If you did not create this account, please ignore this email.</p>
|
|
77
|
-
`,
|
|
78
|
-
});
|
|
79
|
-
return newAccount;
|
|
80
|
-
} catch (error) {
|
|
81
|
-
console.log(error);
|
|
82
|
-
// Rollback the transaction
|
|
83
|
-
await session.abortTransaction();
|
|
84
|
-
throw error;
|
|
85
|
-
} finally {
|
|
86
|
-
session.endSession();
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
// login user
|
|
91
|
-
const login_user_from_db = async (payload: TLoginPayload) => {
|
|
92
|
-
// check account info
|
|
93
|
-
const isExistAccount = await isAccountExist(payload?.email);
|
|
94
|
-
|
|
95
|
-
const isPasswordMatch = await bcrypt.compare(
|
|
96
|
-
payload.password,
|
|
97
|
-
isExistAccount.password,
|
|
98
|
-
);
|
|
99
|
-
if (!isPasswordMatch) {
|
|
100
|
-
throw new AppError("Invalid password", 401);
|
|
101
|
-
}
|
|
102
|
-
const accessToken = jwtHelpers.generateToken(
|
|
103
|
-
{
|
|
104
|
-
email: isExistAccount.email,
|
|
105
|
-
role: isExistAccount.role,
|
|
106
|
-
},
|
|
107
|
-
configs.jwt.access_token as Secret,
|
|
108
|
-
configs.jwt.access_expires as string,
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
const refreshToken = jwtHelpers.generateToken(
|
|
112
|
-
{
|
|
113
|
-
email: isExistAccount.email,
|
|
114
|
-
role: isExistAccount.role,
|
|
115
|
-
},
|
|
116
|
-
configs.jwt.refresh_token as Secret,
|
|
117
|
-
configs.jwt.refresh_expires as string,
|
|
118
|
-
);
|
|
119
|
-
return {
|
|
120
|
-
accessToken: accessToken,
|
|
121
|
-
refreshToken: refreshToken,
|
|
122
|
-
role: isExistAccount.role,
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
const get_my_profile_from_db = async (email: string) => {
|
|
127
|
-
const isExistAccount = await isAccountExist(email);
|
|
128
|
-
const accountProfile = await User_Model.findOne({
|
|
129
|
-
accountId: isExistAccount._id,
|
|
130
|
-
});
|
|
131
|
-
isExistAccount.password = "";
|
|
132
|
-
return {
|
|
133
|
-
account: isExistAccount,
|
|
134
|
-
profile: accountProfile,
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const refresh_token_from_db = async (token: string) => {
|
|
139
|
-
let decodedData;
|
|
140
|
-
try {
|
|
141
|
-
decodedData = jwtHelpers.verifyToken(
|
|
142
|
-
token,
|
|
143
|
-
configs.jwt.refresh_token as Secret,
|
|
144
|
-
);
|
|
145
|
-
} catch (err) {
|
|
146
|
-
throw new Error("You are not authorized!");
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const userData = await Account_Model.findOne({
|
|
150
|
-
email: decodedData.email,
|
|
151
|
-
status: "ACTIVE",
|
|
152
|
-
isDeleted: false,
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
const accessToken = jwtHelpers.generateToken(
|
|
156
|
-
{
|
|
157
|
-
email: userData!.email,
|
|
158
|
-
role: userData!.role,
|
|
159
|
-
},
|
|
160
|
-
configs.jwt.access_token as Secret,
|
|
161
|
-
configs.jwt.access_expires as string,
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
return accessToken;
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
const change_password_from_db = async (
|
|
168
|
-
user: JwtPayload,
|
|
169
|
-
payload: {
|
|
170
|
-
oldPassword: string;
|
|
171
|
-
newPassword: string;
|
|
172
|
-
},
|
|
173
|
-
) => {
|
|
174
|
-
const isExistAccount = await isAccountExist(user?.email);
|
|
175
|
-
|
|
176
|
-
const isCorrectPassword: boolean = await bcrypt.compare(
|
|
177
|
-
payload.oldPassword,
|
|
178
|
-
isExistAccount.password,
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
if (!isCorrectPassword) {
|
|
182
|
-
throw new AppError("Old password is incorrect", 401);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const hashedPassword: string = await bcrypt.hash(payload.newPassword, 10);
|
|
186
|
-
await Account_Model.findOneAndUpdate(
|
|
187
|
-
{ email: isExistAccount.email },
|
|
188
|
-
{
|
|
189
|
-
password: hashedPassword,
|
|
190
|
-
lastPasswordChange: Date(),
|
|
191
|
-
},
|
|
192
|
-
);
|
|
193
|
-
return "Password changed successful.";
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
const forget_password_from_db = async (email: string) => {
|
|
197
|
-
const isAccountExists = await isAccountExist(email);
|
|
198
|
-
const resetToken = jwtHelpers.generateToken(
|
|
199
|
-
{
|
|
200
|
-
email: isAccountExists.email,
|
|
201
|
-
role: isAccountExists.role,
|
|
202
|
-
},
|
|
203
|
-
configs.jwt.reset_secret as Secret,
|
|
204
|
-
configs.jwt.reset_expires as string,
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
const resetPasswordLink = `${configs.jwt.front_end_url}/reset?token=${resetToken}&email=${isAccountExists.email}`;
|
|
208
|
-
const emailTemplate = `<p>Click the link below to reset your password:</p><a href="${resetPasswordLink}">Reset Password</a>`;
|
|
209
|
-
|
|
210
|
-
await sendMail({
|
|
211
|
-
to: email,
|
|
212
|
-
subject: "Password reset successful!",
|
|
213
|
-
textBody: "Your password is successfully reset.",
|
|
214
|
-
htmlBody: emailTemplate,
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
return "Check your email for reset link";
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
const reset_password_into_db = async (
|
|
221
|
-
token: string,
|
|
222
|
-
email: string,
|
|
223
|
-
newPassword: string,
|
|
224
|
-
) => {
|
|
225
|
-
let decodedData: JwtPayload;
|
|
226
|
-
try {
|
|
227
|
-
decodedData = jwtHelpers.verifyToken(
|
|
228
|
-
token,
|
|
229
|
-
configs.jwt.reset_secret as Secret,
|
|
230
|
-
);
|
|
231
|
-
} catch (err) {
|
|
232
|
-
throw new AppError(
|
|
233
|
-
"Your reset link is expire. Submit new link request!!",
|
|
234
|
-
403,
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const isAccountExists = await isAccountExist(email);
|
|
239
|
-
|
|
240
|
-
const hashedPassword: string = await bcrypt.hash(newPassword, 10);
|
|
241
|
-
|
|
242
|
-
await Account_Model.findOneAndUpdate(
|
|
243
|
-
{ email: isAccountExists.email },
|
|
244
|
-
{
|
|
245
|
-
password: hashedPassword,
|
|
246
|
-
lastPasswordChange: Date(),
|
|
247
|
-
},
|
|
248
|
-
);
|
|
249
|
-
return "Password reset successfully!";
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
const verified_account_into_db = async (token: string) => {
|
|
253
|
-
try {
|
|
254
|
-
const { email } = jwtHelpers.verifyToken(
|
|
255
|
-
token,
|
|
256
|
-
configs.jwt.verified_token as string,
|
|
257
|
-
);
|
|
258
|
-
// check account is already verified or blocked
|
|
259
|
-
const isExistAccount = await Account_Model.findOne({ email });
|
|
260
|
-
// check account
|
|
261
|
-
if (!isExistAccount) {
|
|
262
|
-
throw new AppError("Account not found!!", 404);
|
|
263
|
-
}
|
|
264
|
-
if (isExistAccount.isDeleted) {
|
|
265
|
-
throw new AppError("Account deleted !!", 403);
|
|
266
|
-
}
|
|
267
|
-
const result = await Account_Model.findOneAndUpdate(
|
|
268
|
-
{ email },
|
|
269
|
-
{ isVerified: true },
|
|
270
|
-
{ new: true },
|
|
271
|
-
);
|
|
272
|
-
|
|
273
|
-
return result;
|
|
274
|
-
} catch (error) {
|
|
275
|
-
throw new AppError("Invalid or Expired token!!!", 403);
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const get_new_verification_link_from_db = async (email: string) => {
|
|
280
|
-
const isExistAccount = await isAccountExist(email);
|
|
281
|
-
|
|
282
|
-
const verifiedToken = jwtHelpers.generateToken(
|
|
283
|
-
{
|
|
284
|
-
email,
|
|
285
|
-
},
|
|
286
|
-
configs.jwt.verified_token as Secret,
|
|
287
|
-
"5m",
|
|
288
|
-
);
|
|
289
|
-
const verificationLink = `${configs.jwt.front_end_url}/verified?token=${verifiedToken}`;
|
|
290
|
-
await sendMail({
|
|
291
|
-
to: email,
|
|
292
|
-
subject: "New Verification link",
|
|
293
|
-
textBody: `New Account verification link is successfully created on ${new Date().toLocaleDateString()}`,
|
|
294
|
-
htmlBody: `
|
|
295
|
-
<p>Thanks for creating an account with us. We’re excited to have you on board! Click the button below to
|
|
296
|
-
verify your email and activate your account:</p>
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
<div style="text-align: center; margin: 30px 0;">
|
|
300
|
-
<a href="${verificationLink}" target="_blank"
|
|
301
|
-
style="background-color: #4CAF50; color: #ffffff; padding: 14px 28px; text-decoration: none; font-weight: bold; border-radius: 5px; display: inline-block; font-size: 18px;"
|
|
302
|
-
class="btn">
|
|
303
|
-
Verify My Email
|
|
304
|
-
</a>
|
|
305
|
-
</div>
|
|
306
|
-
|
|
307
|
-
<p>If you did not create this account, please ignore this email.</p>
|
|
308
|
-
`,
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
return null;
|
|
312
|
-
};
|
|
313
|
-
|
|
314
|
-
export const auth_services = {
|
|
315
|
-
register_user_into_db,
|
|
316
|
-
login_user_from_db,
|
|
317
|
-
get_my_profile_from_db,
|
|
318
|
-
refresh_token_from_db,
|
|
319
|
-
change_password_from_db,
|
|
320
|
-
forget_password_from_db,
|
|
321
|
-
reset_password_into_db,
|
|
322
|
-
verified_account_into_db,
|
|
323
|
-
get_new_verification_link_from_db,
|
|
324
|
-
};
|