-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
148 lines (122 loc) · 3.46 KB
/
index.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
// https://eco-website-y698.onrender.com/
import ejs from "ejs";
import nodemailer from 'nodemailer';
import express from "express";
// import serverless from 'serverless-http;
import bodyParser from "body-parser";
import { dirname } from "path";
import { fileURLToPath } from "url"
import mongoose from "mongoose"
const __dirname = dirname(fileURLToPath(
import.meta.url));
const app = express();
const port = process.env.PORT || 5500;
const senderMail = "[email protected]";
const password = "qcxs lfxl iyfl witc";
let otp="";
let userEmail="";
let userPassword="";
// let userEnteredOtp="123456";
app.set('view engine', 'ejs');
// mongoose.connect("mongodb://127.0.0.1:27017/ECO", { useNewUrlParser: true });
const uri = 'mongodb://localhost:27017/test';
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
autoIndex: false, // Don't build indexes
maxPoolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
}
const connectWithDB = () => {
mongoose.connect(uri, options, (err, db) => {
if (err) console.error(err);
else console.log("database connection")
})
}
connectWithDB()
const userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
});
const User = mongoose.model("User", userSchema);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname));
app.post("/login", async (req, res) => {
const user = await User.findOne({ email: req.body["email"] }).exec();
if (user) {
if (req.body["password"] == user.password) {
res.sendFile(__dirname + "/index.html");
}
else {
res.render('index', {
existingText: "",
spanText: "Wrong Password!"
});
}
}
else
{
res.render('index', {
existingText: "",
spanText: "User doesn't exists! Please sign-up."
});
}
})
app.post("/signup", async (req, res) => {
const existing = await User.findOne({ email: req.body["email"] }).exec();
if (existing) {
res.render('index', {
spanText: "E-mail ID already registered. Please login",
});
}
else if (req.body["password"] == req.body["conf_password"]) {
userEmail = req.body.email;
userPassword = req.body.password;
otp = Math.floor(100000 + Math.random() * 900000);
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: senderMail,
pass: password,
},
});
const mailOptions = {
from: senderMail,
to: userEmail,
subject: 'OTP for Email Verification',
text: `Your OTP code is ${otp}.`,
};
await transporter.sendMail(mailOptions);
res.render('otp',{spanText1:"",spanText2:"OTP succesfully sent!"});
} else {
res.send("PASSWORDS DON'T MATCH");
}
}
);
app.post("/OTP", async(req,res)=>{
const userOTP = req.body.n1 + req.body.n2 + req.body.n3 + req.body.n4 + req.body.n5 + req.body.n6;
if(userOTP == otp){
const newUser = new User({
email: userEmail,
password: userPassword
});
await newUser.save();
res.sendFile(__dirname + "/index.html");
}
else{
res.render('otp',{spanText1: "Incorrect OTP!",spanText2:""});
}
});
app.listen(port, () => {
console.log(`${port}`);
})