-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (122 loc) · 3.25 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
var express = require('express');
var router = express.Router();
var User = require('../models/user');
router.get('/', function (req, res, next) {
return res.render('index.ejs');
});
router.post('/', function(req, res, next) {
console.log(req.body);
var personInfo = req.body;
if(!personInfo.email || !personInfo.username || !personInfo.password || !personInfo.passwordConf){
res.send();
} else {
if (personInfo.password == personInfo.passwordConf) {
User.findOne({email:personInfo.email},function(err,data){
if(!data){
var c;
User.findOne({},function(err,data){
if (data) {
console.log("if");
c = data.unique_id + 1;
}else{
c=1;
}
var newPerson = new User({
unique_id:c,
email:personInfo.email,
username: personInfo.username,
password: personInfo.password,
passwordConf: personInfo.passwordConf
});
newPerson.save(function(err, Person){
if(err)
console.log(err);
else
console.log('Success');
});
}).sort({_id: -1}).limit(1);
res.send({"Success":"You are regestered,You can login now."});
}else{
res.send({"Success":"Email is already used."});
}
});
}else{
res.send({"Success":"password is not matched"});
}
}
});
router.get('/login', function (req, res, next) {
return res.render('login.ejs');
});
router.post('/login', function (req, res, next) {
//console.log(req.body);
User.findOne({email:req.body.email},function(err,data){
if(data){
if(data.password==req.body.password){
//console.log("Done Login");
req.session.userId = data.unique_id;
//console.log(req.session.userId);
res.send({"Success":"Success!"});
}else{
res.send({"Success":"Wrong password!"});
}
}else{
res.send({"Success":"This Email Is not regestered!"});
}
});
});
router.get('/profile', function (req, res, next) {
console.log("profile");
User.findOne({unique_id:req.session.userId},function(err,data){
console.log("data");
console.log(data);
if(!data){
res.redirect('/');
}else{
//console.log("found");
return res.render('data.ejs', {"name":data.username,"email":data.email});
}
});
});
router.get('/logout', function (req, res, next) {
console.log("logout")
if (req.session) {
// delete session object
req.session.destroy(function (err) {
if (err) {
return next(err);
} else {
return res.redirect('/');
}
});
}
});
router.get('/forgetpass', function (req, res, next) {
res.render("forget.ejs");
});
router.post('/forgetpass', function (req, res, next) {
//console.log('req.body');
//console.log(req.body);
User.findOne({email:req.body.email},function(err,data){
console.log(data);
if(!data){
res.send({"Success":"This Email Is not regestered!"});
}else{
// res.send({"Success":"Success!"});
if (req.body.password==req.body.passwordConf) {
data.password=req.body.password;
data.passwordConf=req.body.passwordConf;
data.save(function(err, Person){
if(err)
console.log(err);
else
console.log('Success');
res.send({"Success":"Password changed!"});
});
}else{
res.send({"Success":"Password does not matched! Both Password should be same."});
}
}
});
});
module.exports = router;