-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
168 lines (136 loc) · 3.62 KB
/
app.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
const express = require('express')
const mongoose = require('mongoose');
const session = require("express-session");
const app = express();
app.use(express.json());
app.use (
session ({
secret: "53cr3t50m3th1ng",
resave: true,
rolling: true,
saveUninitialized: false,
cookie: {
maxAge: 30 * 1000
}
})
);
const User = require('./model/user');
const bcrypt = require("bcrypt");
const nodemailer = require("nodemailer");
mongoose.connect('mongodb://username:[email protected]:61134/signup_otp', { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false }, (req, res) => {
console.log("Connected to database");
})
app.post('/signup', (req, res) => {
User.find({ email: req.body.email })
.then(user => {
if (user.length >= 1) {
return res.json({
message: "Mail exists"
});
} else {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'emailid',
pass: "apppassword"
},
tls: {
rejectUnauthorized: false,
}
});
let otpValue = Math.floor(100000 + Math.random() * 9000);
const newUser = new User({
fname: req.body.firstname,
lname: req.body.lastname,
email: req.body.email,
otp: otpValue,
status: "pending",
password: ''
})
try {
newUser.save();
// mongoose.save(newUser);
res.send("Sign up successful");
} catch (err) {
console.log(err)
}
let mailOptions = {
from: '[email protected]',
to: req.body.email,
//to: '[email protected]',
subject: 'Otp for Confirmation',
text: `Dear ${req.body.firstname},Otp for your confirmation is ${otpValue}`,
//attachments:[{filename:newname}]
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
// res.send("somthing went wrong")
} else {
console.log('Email sent: ' + info.response);
}
});
}
})
})
app.post('/verify', (req, res) => {
if(req.body.password != req.body.password2){
return res.json({
message:"Passowrd did not match"
})
}
if(req.body.password.length < 4 ){
return res.json({
message:"password must be atleast 4 characters"
})
}
bcrypt.hash(req.body.password, 10, (err, hash) => {
if (err) {
return res.json({
error: err
});
}
User.findOneAndUpdate({ otp: parseInt(req.body.otp) }, { $set: { status: "Success", password: hash }, $unset: { otp: 1 } }, { new: true }, (err, doc) => {
if (doc == null) {
res.send("Wrong otp")
}
else {
res.send(doc);
}
});
})
})
app.post("/login", (req, res) => {
User.find({ email: req.body.email })
.then(user => {
if (user.length < 1) {
return res.json({
message: "Auth failed"
});
}
bcrypt.compare(req.body.password, user[0].password, (err, result) => {
if (err) {
res.status(401).json({
message: "Auth failed"
});
}
if (result && user[0].status == 'Successs') {
req.session.userId = user[0]._id
return res.json({
message: "Auth successful",
});
}else{
res.json({
message: "Otp not verified"
});
}
});
})
.catch(err => {
console.log(err);
res.send(err);
});
});
app.listen(3000, () => {
console.log("Server port running on 3000");
})