-
Notifications
You must be signed in to change notification settings - Fork 0
/
userAuth.js
213 lines (195 loc) · 6.77 KB
/
userAuth.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const router = require("express").Router();
const { MongoClient, ObjectID } = require("mongodb");
const bcrypt = require("bcrypt");
const nodeMailer = require("nodemailer");
var jwt = require('jsonwebtoken')
var randomstring = require("randomstring");
require("dotenv").config();
const dbUrl = process.env.DB_URL;
const feUrl = process.env.FE_URL;
const randomString = randomstring.generate();
router.post("/register", async (req, res) => {
try {
let client = await MongoClient.connect(dbUrl);
let db = client.db("Url-Shortener");
let data = await db.collection("users").findOne({ email: req.body.email });
if (!data) {
let hashedPassword = await bcrypt.hash(req.body.password, 10);
req.body.password = hashedPassword;
let regToken = jwt.sign({email: req.body.email},process.env.REG_SECRET_KEY);
await db.collection("users").insertOne({ firstname:req.body.firstname, lastname:req.body.lastname, email:req.body.email, password:req.body.password, active:false, regToken:regToken});
let mailTransporter = nodeMailer.createTransport({
host: "smtp-mail.outlook.com",
port: 587,
secure: false,
tls: {
rejectUnauthorized: false,
},
auth: {
user: "dineshdevlpr@outlook.com",
pass: process.env.PASS
},
});
let mailDetails = await mailTransporter.sendMail({
from: '"DINESH"<dineshdevlpr@outlook.com>',
to: req.body.email,
subject: "Account Activation link",
html: `<div>
<h3>Thanks for Registering.. Click on the below link to activate your account :)</h3>
<a href="${feUrl+"activation/"+regToken}">Activate Account</a>
</div>`,
});
console.log(mailDetails);
res.status(200).send("Check your registered mail for activation link");
} else {
res.status(400).json({
message: `User With ${req.body.email} Already Exists. Try Using Login Option`,
});
}
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
router.put("/activation/:token", async(req,res)=>{
try{
let client = await MongoClient.connect(dbUrl);
let db = client.db("Url-Shortener");
let data = await db.collection("users").findOne({ regToken:req.params.token });
if(data){
await db.collection("users").updateOne({ regToken:req.params.token },{ $unset : { regToken:1 }, $set : { active:true }});
res.status(200).send("Account Successfully Activated");
}
else
res.status(404).send("Invalid Request");
}
catch (error) {
console.log(error);
res.status(500).json({
message: "Internal Server Error",
});
}
});
router.post("/login", async (req, res) => {
try {
let client = await MongoClient.connect(dbUrl);
let db = client.db("Url-Shortener");
let data = await db.collection("users").findOne({ email: req.body.email });
if (data) {
if (data.active===true) {
let isValid = await bcrypt.compare(req.body.password, data.password);
if (isValid) {
let authToken = jwt.sign({user_id:data._id},process.env.AUTH_KEY)
res.status(200).json({
message: "Successfully Logged In", authToken
});
} else {
res.status(401).json({
message: "Invalid Password",
});
}
} else {
res.status(400).json({
message : `${req.body.email} is not yet activated. Check Your mail to activate`
});
}
}
else {
res.status(400).json({
message: `${req.body.email} not yet Registered`,
});
}
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
router.post("/forgot", async (req, res) => {
try {
let client = await MongoClient.connect(dbUrl);
let db = client.db("Url-Shortener");
let data = await db
.collection("users")
.findOne({ email: req.body.email });
if (data) {
await db
.collection("users")
.findOneAndUpdate(
{ email: req.body.email },
{ $set: { randomString : randomString } }
);
let mailTransporter = nodeMailer.createTransport({
host: "smtp-mail.outlook.com",
port: 587,
secure: false,
tls: {
rejectUnauthorized: false,
},
auth: {
user: "dineshdevlpr@outlook.com",
pass: process.env.PASS
}
});
let mailDetails = await mailTransporter.sendMail({
from: '"DINESH"<dineshdevlpr@outlook.com>',
to: req.body.email,
subject: "Password Reset",
html: `<div>
<h3>If You have requested for Password Reset, Click the following link to reset your password.If not requested, then just ignore this mail</h3>
<a href="${feUrl+"reset/"+randomString}">RESET PASSWORD</a>
</div>`,
})
console.log(mailDetails)
res.status(200).json({
message:
"Password Reset Link has been sent to your mail",
});
} else {
res.status(404).json({
message: "User not found",
});
}
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
router.post("/reset/:randomString", async (req, res) => {
try {
const client = await MongoClient.connect(dbUrl, {
useUnifiedTopology: true,
});
const db = client.db("Url-Shortener");
const userData = await db.collection("users").findOne({
randomString: req.params.randomString
});
if (userData) {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const updated = await db
.collection("users")
.updateOne(
{ randomString: req.params.randomString },
{ $set: { password: hashedPassword } }
);
if (updated) {
await db
.collection("users")
.updateOne(
{ randomString: req.params.randomString },
{ $unset: { randomString : 1} }
);
res.status(200).json({
message: "Password Successfully updated",
});
}
} else {
res.status(404).json({
message: "Error Password can't be updated",
});
}
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
module.exports = router;