exp-node-server 1.1.1 → 1.1.2
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/package.json
CHANGED
|
@@ -4,29 +4,29 @@ import manageResponse from "../../utils/manage_response";
|
|
|
4
4
|
import { auth_services } from "./auth.service";
|
|
5
5
|
|
|
6
6
|
const register_user = catchAsync(async (req, res) => {
|
|
7
|
-
const result = await auth_services.register_user_into_db(req?.body)
|
|
7
|
+
const result = await auth_services.register_user_into_db(req?.body);
|
|
8
8
|
manageResponse(res, {
|
|
9
9
|
success: true,
|
|
10
10
|
message: "Account created successful",
|
|
11
|
-
statusCode:
|
|
12
|
-
data: result
|
|
13
|
-
})
|
|
14
|
-
})
|
|
11
|
+
statusCode: 200,
|
|
12
|
+
data: result,
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
15
|
|
|
16
16
|
const login_user = catchAsync(async (req, res) => {
|
|
17
17
|
const result = await auth_services.login_user_from_db(req.body);
|
|
18
18
|
|
|
19
|
-
res.cookie(
|
|
20
|
-
secure: configs.env ==
|
|
19
|
+
res.cookie("refreshToken", result.refreshToken, {
|
|
20
|
+
secure: configs.env == "production",
|
|
21
21
|
httpOnly: true,
|
|
22
22
|
});
|
|
23
23
|
manageResponse(res, {
|
|
24
|
-
statusCode:
|
|
24
|
+
statusCode: 200,
|
|
25
25
|
success: true,
|
|
26
|
-
message:
|
|
26
|
+
message: "User is logged in successful !",
|
|
27
27
|
data: {
|
|
28
28
|
accessToken: result.accessToken,
|
|
29
|
-
role: result?.role
|
|
29
|
+
role: result?.role,
|
|
30
30
|
},
|
|
31
31
|
});
|
|
32
32
|
});
|
|
@@ -35,9 +35,9 @@ const get_my_profile = catchAsync(async (req, res) => {
|
|
|
35
35
|
const { email } = req.user!;
|
|
36
36
|
const result = await auth_services.get_my_profile_from_db(email);
|
|
37
37
|
manageResponse(res, {
|
|
38
|
-
statusCode:
|
|
38
|
+
statusCode: 200,
|
|
39
39
|
success: true,
|
|
40
|
-
message:
|
|
40
|
+
message: "User profile fetched successfully!",
|
|
41
41
|
data: result,
|
|
42
42
|
});
|
|
43
43
|
});
|
|
@@ -46,9 +46,9 @@ const refresh_token = catchAsync(async (req, res) => {
|
|
|
46
46
|
const { refreshToken } = req.cookies;
|
|
47
47
|
const result = await auth_services.refresh_token_from_db(refreshToken);
|
|
48
48
|
manageResponse(res, {
|
|
49
|
-
statusCode:
|
|
49
|
+
statusCode: 200,
|
|
50
50
|
success: true,
|
|
51
|
-
message:
|
|
51
|
+
message: "Refresh token generated successfully!",
|
|
52
52
|
data: result,
|
|
53
53
|
});
|
|
54
54
|
});
|
|
@@ -58,20 +58,20 @@ const change_password = catchAsync(async (req, res) => {
|
|
|
58
58
|
const result = await auth_services.change_password_from_db(user!, req.body);
|
|
59
59
|
|
|
60
60
|
manageResponse(res, {
|
|
61
|
-
statusCode:
|
|
61
|
+
statusCode: 200,
|
|
62
62
|
success: true,
|
|
63
|
-
message:
|
|
63
|
+
message: "Password changed successfully!",
|
|
64
64
|
data: result,
|
|
65
65
|
});
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
const forget_password = catchAsync(async (req, res) => {
|
|
69
|
-
const { email } = req?.body
|
|
69
|
+
const { email } = req?.body;
|
|
70
70
|
await auth_services.forget_password_from_db(email);
|
|
71
71
|
manageResponse(res, {
|
|
72
|
-
statusCode:
|
|
72
|
+
statusCode: 200,
|
|
73
73
|
success: true,
|
|
74
|
-
message:
|
|
74
|
+
message: "Reset password link sent to your email!",
|
|
75
75
|
data: null,
|
|
76
76
|
});
|
|
77
77
|
});
|
|
@@ -84,33 +84,35 @@ const reset_password = catchAsync(async (req, res) => {
|
|
|
84
84
|
newPassword,
|
|
85
85
|
);
|
|
86
86
|
manageResponse(res, {
|
|
87
|
-
statusCode:
|
|
87
|
+
statusCode: 200,
|
|
88
88
|
success: true,
|
|
89
|
-
message:
|
|
89
|
+
message: "Password reset successfully!",
|
|
90
90
|
data: result,
|
|
91
91
|
});
|
|
92
92
|
});
|
|
93
93
|
|
|
94
94
|
const verified_account = catchAsync(async (req, res) => {
|
|
95
|
-
const result = await auth_services.verified_account_into_db(req?.body?.token)
|
|
95
|
+
const result = await auth_services.verified_account_into_db(req?.body?.token);
|
|
96
96
|
|
|
97
97
|
manageResponse(res, {
|
|
98
|
-
statusCode:
|
|
98
|
+
statusCode: 200,
|
|
99
99
|
success: true,
|
|
100
100
|
message: "Account Verification successful.",
|
|
101
|
-
data: result
|
|
102
|
-
})
|
|
103
|
-
})
|
|
101
|
+
data: result,
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
104
|
|
|
105
105
|
const get_new_verification_link = catchAsync(async (req, res) => {
|
|
106
|
-
const result = await auth_services.get_new_verification_link_from_db(
|
|
106
|
+
const result = await auth_services.get_new_verification_link_from_db(
|
|
107
|
+
req?.body?.email,
|
|
108
|
+
);
|
|
107
109
|
manageResponse(res, {
|
|
108
|
-
statusCode:
|
|
110
|
+
statusCode: 200,
|
|
109
111
|
success: true,
|
|
110
112
|
message: "New Verification link is send on email.",
|
|
111
|
-
data: result
|
|
112
|
-
})
|
|
113
|
-
})
|
|
113
|
+
data: result,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
114
116
|
|
|
115
117
|
export const auth_controllers = {
|
|
116
118
|
register_user,
|
|
@@ -121,5 +123,5 @@ export const auth_controllers = {
|
|
|
121
123
|
reset_password,
|
|
122
124
|
forget_password,
|
|
123
125
|
verified_account,
|
|
124
|
-
get_new_verification_link
|
|
125
|
-
}
|
|
126
|
+
get_new_verification_link,
|
|
127
|
+
};
|
|
@@ -1,64 +1,66 @@
|
|
|
1
|
-
import { AppError } from "../../utils/app_error";
|
|
2
|
-
import { TAccount, TLoginPayload, TRegisterPayload } from "./auth.interface";
|
|
3
|
-
import { Account_Model } from "./auth.schema";
|
|
4
1
|
import bcrypt from "bcrypt";
|
|
5
|
-
import {
|
|
6
|
-
import { User_Model } from "../user/user.schema";
|
|
2
|
+
import { JwtPayload, Secret } from "jsonwebtoken";
|
|
7
3
|
import mongoose from "mongoose";
|
|
8
|
-
import { jwtHelpers } from "../../utils/JWT";
|
|
9
4
|
import { configs } from "../../configs";
|
|
10
|
-
import {
|
|
11
|
-
import sendMail from "../../utils/mail_sender";
|
|
5
|
+
import { AppError } from "../../utils/app_error";
|
|
12
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
13
|
// register user
|
|
14
14
|
const register_user_into_db = async (payload: TRegisterPayload) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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: `
|
|
62
64
|
<p>Thanks for creating an account with us. We’re excited to have you on board! Click the button below to
|
|
63
65
|
verify your email and activate your account:</p>
|
|
64
66
|
|
|
@@ -72,208 +74,224 @@ const register_user_into_db = async (payload: TRegisterPayload) => {
|
|
|
72
74
|
</div>
|
|
73
75
|
|
|
74
76
|
<p>If you did not create this account, please ignore this email.</p>
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
+
}
|
|
86
88
|
};
|
|
87
89
|
|
|
88
|
-
|
|
89
90
|
// login user
|
|
90
91
|
const login_user_from_db = async (payload: TLoginPayload) => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
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
125
|
|
|
126
126
|
const get_my_profile_from_db = async (email: string) => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
isExistAccount.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
+
};
|
|
134
136
|
};
|
|
135
137
|
|
|
136
138
|
const refresh_token_from_db = async (token: string) => {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
);
|
|
143
|
-
} catch (err) {
|
|
144
|
-
throw new Error('You are not authorized!');
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const userData = await Account_Model.findOne({ email: decodedData.email, status: "ACTIVE", isDeleted: false })
|
|
148
|
-
|
|
149
|
-
const accessToken = jwtHelpers.generateToken(
|
|
150
|
-
{
|
|
151
|
-
email: userData!.email,
|
|
152
|
-
role: userData!.role,
|
|
153
|
-
},
|
|
154
|
-
configs.jwt.access_token as Secret,
|
|
155
|
-
configs.jwt.access_expires as string,
|
|
139
|
+
let decodedData;
|
|
140
|
+
try {
|
|
141
|
+
decodedData = jwtHelpers.verifyToken(
|
|
142
|
+
token,
|
|
143
|
+
configs.jwt.refresh_token as Secret,
|
|
156
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
|
+
);
|
|
157
163
|
|
|
158
|
-
|
|
164
|
+
return accessToken;
|
|
159
165
|
};
|
|
160
166
|
|
|
161
167
|
const change_password_from_db = async (
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
168
|
+
user: JwtPayload,
|
|
169
|
+
payload: {
|
|
170
|
+
oldPassword: string;
|
|
171
|
+
newPassword: string;
|
|
172
|
+
},
|
|
167
173
|
) => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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.";
|
|
185
194
|
};
|
|
186
195
|
|
|
187
196
|
const forget_password_from_db = async (email: string) => {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
+
);
|
|
197
206
|
|
|
198
|
-
|
|
199
|
-
|
|
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>`;
|
|
200
209
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
210
|
+
await sendMail({
|
|
211
|
+
to: email,
|
|
212
|
+
subject: "Password reset successful!",
|
|
213
|
+
textBody: "Your password is successfully reset.",
|
|
214
|
+
htmlBody: emailTemplate,
|
|
215
|
+
});
|
|
207
216
|
|
|
208
|
-
|
|
217
|
+
return "Check your email for reset link";
|
|
209
218
|
};
|
|
210
219
|
|
|
211
220
|
const reset_password_into_db = async (
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
221
|
+
token: string,
|
|
222
|
+
email: string,
|
|
223
|
+
newPassword: string,
|
|
215
224
|
) => {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
+
}
|
|
228
237
|
|
|
229
|
-
|
|
238
|
+
const isAccountExists = await isAccountExist(email);
|
|
230
239
|
|
|
231
|
-
|
|
240
|
+
const hashedPassword: string = await bcrypt.hash(newPassword, 10);
|
|
232
241
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
242
|
+
await Account_Model.findOneAndUpdate(
|
|
243
|
+
{ email: isAccountExists.email },
|
|
244
|
+
{
|
|
245
|
+
password: hashedPassword,
|
|
246
|
+
lastPasswordChange: Date(),
|
|
247
|
+
},
|
|
248
|
+
);
|
|
249
|
+
return "Password reset successfully!";
|
|
238
250
|
};
|
|
239
251
|
|
|
240
252
|
const verified_account_into_db = async (token: string) => {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
return result
|
|
255
|
-
} catch (error) {
|
|
256
|
-
throw new AppError("Invalid or Expired token!!!", httpStatus.BAD_REQUEST)
|
|
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);
|
|
257
266
|
}
|
|
267
|
+
const result = await Account_Model.findOneAndUpdate(
|
|
268
|
+
{ email },
|
|
269
|
+
{ isVerified: true },
|
|
270
|
+
{ new: true },
|
|
271
|
+
);
|
|
258
272
|
|
|
259
|
-
|
|
273
|
+
return result;
|
|
274
|
+
} catch (error) {
|
|
275
|
+
throw new AppError("Invalid or Expired token!!!", 403);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
260
278
|
|
|
261
279
|
const get_new_verification_link_from_db = async (email: string) => {
|
|
262
|
-
|
|
280
|
+
const isExistAccount = await isAccountExist(email);
|
|
263
281
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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: `
|
|
277
295
|
<p>Thanks for creating an account with us. We’re excited to have you on board! Click the button below to
|
|
278
296
|
verify your email and activate your account:</p>
|
|
279
297
|
|
|
@@ -287,20 +305,20 @@ const get_new_verification_link_from_db = async (email: string) => {
|
|
|
287
305
|
</div>
|
|
288
306
|
|
|
289
307
|
<p>If you did not create this account, please ignore this email.</p>
|
|
290
|
-
|
|
291
|
-
|
|
308
|
+
`,
|
|
309
|
+
});
|
|
292
310
|
|
|
293
|
-
|
|
294
|
-
}
|
|
311
|
+
return null;
|
|
312
|
+
};
|
|
295
313
|
|
|
296
314
|
export const auth_services = {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}
|
|
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
|
+
};
|