-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarbage.txt
61 lines (48 loc) · 1.65 KB
/
garbage.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
// if (!data.verfied) {
// const token = await tokenModel.findOne({ userId: data._id });
// if (!token) {
// const newtoken = await tokenModel.create({
// userId: data._id,
// token: crypto.randomBytes(32).toString("hex"),
// });
// const url = `${process.env.BASE_URL}users/${data._id}/verify/${newtoken.token}`;
// await sendEmail(data.email, "Verify email", url);
// }
// return res.status(401).send({
// message:
// "Email has been sent to your gmail account after verifying try logging in again",
// });
// }
const token = await tokenModel.create({
userId: huru._id,
token: crypto.randomBytes(32).toString("hex"),
});
const url = `${process.env.BASE_URL}users/${huru._id}/verify/${token.token}`;
await sendEmail(huru.email, "Verify email", url);
app.get("/:id/verify/:token", async (req, res) => {
const { id, token } = req.params;
try {
const data = await userModel.findOne({ _id: id });
if (!data) {
return res.status(401).send({ message: "Invalid link" });
}
const exixtingtoken = await tokenModel.findOne({
userId: id,
token: token,
});
if (!exixtingtoken) {
return res.status(401).send({ message: "Invalid link" });
}
await userModel.findOneAndUpdate(
{ _id: data._id },
{ $set: { verfied: true } }
);
await exixtingtoken.remove();
res.send({ message: "Email Verified successfully" });
} catch (error) {
console.log("error:", error);
res
.status(401)
.send({ message: "Internal server error please try again later" });
}
});