-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
218 lines (210 loc) · 5.47 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
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
214
215
216
217
218
var express = require("express");
var bodyParser = require("body-parser");
var fast2sms = require("fast-two-sms");
require("dotenv").config();
var mongoose = require("mongoose");
var multer = require("multer");
const User = require("./modal/regis");
const bcrypt = require("bcryptjs");
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, "./uploads");
},
filename: function(req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({ storage: storage });
const app = express();
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.engine("html", require("ejs").renderFile);
app.set("view engine", "html");
app.set("views", "public");
mongoose.connect("mongodb://Localhost:27017/logs", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
var db = mongoose.connection;
db.on("error", () => console.log("Error in connecting to Mongo"));
db.once("open", () => console.log("Connected to database"));
app.post("/signup", upload.single("avatar"), async (req, res, cb) => {
// console.log(req.file);
console.log(req.body.number);
usernumberreg = await User.findOne({ number: req.body.number }).lean();
if (usernumberreg == null) {
var firstName = req.body.first;
var lastName = req.body.last;
var username = req.body.username;
var password = req.body.password;
var number = req.body.number;
var password = await bcrypt.hash(password, 10);
data = {
firstname: firstName,
lastname: lastName,
username: username,
password: password,
number: number
};
otp = Math.floor(1000 + Math.random() * 9000);
console.log(otp);
const response = await fast2sms.sendMessage({
authorization: process.env.API_KEY,
message: otp,
numbers: [req.body.number]
});
console.log(response);
return res.render("otp.html");
} else {
res.render("noreg.html", {
name: "dear user",
message: "this number is already registered",
bod: "login",
linkm: "go to login",
title: "Already registered"
});
}
});
app.post("/otpind", async (req, res) => {
var otpen = req.body.otpin;
console.log(otpen, otp);
if (otpen == otp) {
console.log(data);
try {
const respon = await User.create(data);
console.log(`successfully registered ${respon}`);
} catch (e) {
// console.log(e);
if (e.code === 11000) {
console.log("Username taken");
}
throw e;
}
return res.redirect("finish.html");
} else {
otp = Math.floor(1000 + Math.random() * 9000);
console.log("try again");
res.redirect("regis.html");
}
});
app.get("/regis", (req, res) => {
console.log("Going to regis page");
res.redirect("regis.html");
});
app.get("/login", (req, res) => {
console.log("Going to login page");
res.redirect("login.html");
});
app.post("/logind", async (req, res) => {
console.log("Going to logind page");
const loginuser = req.body.loginname;
const loginpow = req.body.loginpow;
const userlogin = await User.findOne({ username: loginuser }).lean();
// console.log(userlogin);
if (userlogin != null) {
if (await bcrypt.compare(loginpow, userlogin.password)) {
console.log("logged in");
res.render("noreg.html", {
name: "",
message: "Hi! " + loginuser + " You are logged in",
bod: "login",
linkm: "exit",
title: "Welcome"
});
} else {
console.log("wrong password");
res.render("noreg.html", {
name: loginuser,
message: "your password is incorrect",
bod: "login",
linkm: "Try again",
title: "Wrong password"
});
}
} else {
console.log("not registered");
res.render("noreg.html", {
name: loginuser,
message: "you are not registered",
bod: "regis",
linkm: "Go register yourself",
title: "Not registered"
});
}
});
app.get("/forget", async (req, res) => {
res.render("forget.html");
});
app.post("/forgetotp", async (req, res) => {
usernumber = await User.findOne({ number: req.body.forgetnumber }).lean();
if (usernumber != null) {
otp = Math.floor(1000 + Math.random() * 9000);
console.log(otp);
const response = await fast2sms.sendMessage({
authorization: process.env.API_KEY,
message: otp,
numbers: [req.body.forgetnumber]
});
console.log(response);
res.redirect("otpforget.html");
} else {
res.render("noreg.html", {
name: "dear user",
message: "you are not registered",
bod: "regis",
linkm: "Go register yourself",
title: "Not registered"
});
}
});
app.post("/otpforget", async (req, res) => {
if (otp == req.body.otpinforget) {
res.redirect("newpass.html");
} else {
res.render("noreg.html", {
name: " ",
message: "wrong otp",
bod: "forget",
linkm: "try again",
title: "invalid otp"
});
}
});
app.post("/newpass", async (req, res) => {
console.log(usernumber._id);
// console.log(req.body.newpass);
var newpassword = await bcrypt.hash(req.body.newpass, 10);
console.log(newpassword);
const updatepass = async _id => {
const result = await User.updateOne(
{ _id },
{
$set: {
password: newpassword
}
}
);
console.log("updated successfully");
};
updatepass(usernumber._id);
res.render("noreg.html", {
name: "dear" + usernumber.username,
message: " your password is updated successfully",
bod: "login",
linkm: "Go to login",
title: "password updated"
});
});
app
.get("/", (req, res) => {
res.set({
"Allow-access-Allow-Origin": "*"
});
return res.redirect("login.html");
})
.listen(8080, 'localhost');
console.log("listening");
// abc