8000 creating users done · vishal-coder/CRM_backend@cbee393 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit cbee393

Browse files
committed
creating users done
1 parent 6dd4ae7 commit cbee393

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

controllers/AuthController.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,49 @@ import {
2222
*/
2323
export const signup = async (req, res) => {
2424
console.log("signup requested", req.body);
25-
const { firstname, lastname, phone, address, username, password, about } =
26-
req.body;
25+
const { firstname, lastname, phone, username, userType } = req.body;
2726
const dBUserByEmail = await getDBUserByEmail({ username: username });
2827
if (dBUserByEmail) {
2928
return res.status(401).send({ message: "User Already Exists" });
3029
}
3130

32-
let hashedPassword = await getHashedPassword(password);
3331
const registerResult = await registerUser({
3432
firstname: firstname,
3533
lastname: lastname,
3634
username: username,
37-
password: hashedPassword,
3835
phone: phone,
39-
address: address,
40-
about: about,
36+
userType: userType,
37+
isActive: false,
4138
});
4239

43-
var confirmationToken = await jwt.sign(
44-
{ id: registerResult.insertedId.toString() },
45-
process.env.SECRET_KEY
46-
);
40+
let resetToken = Crypto.randomBytes(16).toString("hex");
41+
let hashedResetToken = await getHashedPassword(resetToken);
42+
43+
let tokenUpdate = await insertToken({ username: username }, hashedResetToken);
44+
if (!tokenUpdate) {
45+
return res.status(401).send({
46+
message: "Something went wront..Please try again later.",
47+
success: false,
48+
});
49+
}
4750

48-
const isInserted = await insertAccountConfirmationCode(
49-
{ username: username },
50-
confirmationToken
51+
console.log("inserted id is", registerResult);
52+
53+
if (!tokenUpdate) {
54+
return res.status(401).send({
55+
message: "Something went wront..Please try again later.",
56+
success: false,
57+
});
58+
}
59+
60+
const mailsuccess = await sendPasswordResetMail(
61+
username,
62+
hashedResetToken,
63+
registerResult.insertedId
5164
);
52-
console.log("isInserted", isInserted);
53-
sendAccountVerificationMail(username, confirmationToken, firstname);
5465

5566
res.status(200).send({
56-
message: "User was registered successfully! Please Verify Your Email!",
67+
message: "User was registered successfully! ",
5768
success: true,
5869
});
5970
};
@@ -98,13 +109,13 @@ export const login = async (req, res) => {
98109
user: {
99110
name: dBUserByEmail.firstname,
100111
email: dBUserByEmail.username,
101-
userType: dBUserByEmail.role,
102-
address: dBUserByEmail.address,
112+
userType: dBUserByEmail.userType,
103113
token: token,
104114
},
105115
});
106116
};
107117

118+
//TODO:-useThis fo 67E6 r user activation
108119
export const forgotPassword = async (req, res) => {
109120
const { email } = req.body;
110121

@@ -155,6 +166,7 @@ export const resetpassword = async (req, res) => {
155166
const query = { token: token };
156167
const updateQuery = { $set: { password: hashedPassword } };
157168
const updatePasswordResult = await updatePassword(query, updateQuery);
169+
const activateUser = await activatateUser(token);
158170

159171
// const deleteTokenResult = await deleteToken(token); TODO: remove this comment and delete below line
160172
const deleteTokenResult = true;

models/AuthModel.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
import { client } from "../index.js";
22

33
export async function getDBUserByEmail(data) {
4-
return client.db("blog").collection("users").findOne(data);
4+
return client.db("CRM").collection("users").findOne(data);
55
}
66

77
export function registerUser(data) {
8-
return client.db("blog").collection("users").insertOne(data);
8+
return client.db("CRM").collection("users").insertOne(data);
99
}
1010

1111
export function insertToken(data, hashedResetToken) {
1212
return client
13-
.db("blog")
13+
.db("CRM")
1414
.collection("users")
1515
.updateOne(data, {
1616
$set: { token: hashedResetToken, createdAt: new Date() },
1717
});
1818
}
1919

2020
export function getToken(data) {
21-
return client.db("blog").collection("users").findOne(data);
21+
return client.db("CRM").collection("users").findOne(data);
2222
}
2323

2424
export function updatePassword(query, updateQuery) {
25-
return client.db("blog").collection("users").updateOne(query, updateQuery);
25+
return client.db("CRM").collection("users").updateOne(query, updateQuery);
2626
}
2727
export function deleteToken(token) {
2828
return client
29-
.db("blog")
29+
.db("CRM")
3030
.collection("users")
3131
.updateOne({ token: token }, { $unset: { token: "" } });
3232
}
3333
export async function verifyEmailToken(data) {
34-
return client.db("blog").collection("users").findOne(data);
34+
return client.db("CRM").collection("users").findOne(data);
3535
}
3636

3737
export function activatateUser(token) {
3838
return client
39-
.db("blog")
39+
.db("CRM")
4040
.collection("users")
41-
.updateOne({ confirmationToken: token }, { $set: { isActive: true } });
41+
.updateOne({ token: token }, { $set: { isActive: true } });
4242
}
4343

4444
export function insertAccountConfirmationCode(data, confirmationToken) {
4545
return client
46-
.db("blog")
46+
.db("CRM")
4747
.collection("users")
4848
.updateOne(data, {
4949
$set: { confirmationToken: confirmationToken },
5050
});
5151
}
5252

5353
export async function isUserActive(data) {
54-
return client.db("blog").collection("users").findOne(data);
54+
return client.db("CRM").collection("users").findOne(data);
5555
}

validations/AuthValidation.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ export function signupValidation() {
77
check("firstname").notEmpty(),
88
check("lastname").notEmpty(),
99
check("phone").notEmpty(),
10-
check("address").notEmpty(),
1110
check("username", "Please provide a valid email as username").isEmail(),
12-
check("password", "Password length must be greater than 6 ").isLength({
13-
min: 6,
14-
}),
11+
1512
(req, res, next) => {
1613
const errors = validationResult(req);
1714

0 commit comments

Comments
 (0)
0