-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
194 lines (166 loc) · 3.98 KB
/
server.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
var express = require('express');
var bodyPaeser = require('body-parser');
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
var csrf = require('csurf');
// way 1 of managing session(https://www.npmjs.com/package/express-session)
//var session = require('express-session');
// way 2 of managing session(https://www.npmjs.com/package/client-sessions)
var session = require('client-sessions');
// other way of managing session
/*https://www.npmjs.com/package/cookie-session
https://www.npmjs.com/package/client-session
https://www.npmjs.com/package/cookie-parser
*/
// database connectivity and schema definition
mongoose.connect('mongodb://localhost/testdb');
var Schema = mongoose.Schema;
var userSchema = new Schema({
username : String,
email : String,
firstname : String,
lastname :String,
password : String
});
var User = mongoose.model('users', userSchema);
var app = express();
app.set('views', __dirname + '/view');
app.set('view engine', 'ejs');
// middleware
app.use(bodyPaeser.urlencoded({extended:true}));
// way 1 of managing session
/*app.use(session({
secret : "superSecret",
saveUninitialized : true,
resave : true,
cookie : { // these are default values, we can change per our need
path: '/',
httpOnly: true,
secure: false,
maxAge: null
}
}));*/
// way 2 of managing session
app.use(session({
cookieName: 'session', // cookie name dictates the key name added to the request object
secret: 'superSecret', // should be a large unguessable string
duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
activeDuration: 1000 * 60 * 5, // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds
httpOnly: true,
secure: true
}));
app.use(csrf());
app.use(function(req, res, next){
res.locals.csrfToken = req.csrfToken();
next();
});
/**
* Session Middleware
*/
app.use(function(req, res, next){
if(req.session && req.session.user){
// lookup the user in the DB by pulling their email from the session
User.findOne({'email':req.session.user.email}, function(err, user){
if(err){
res.redirect('/login');
}
if(!user){
req.session.reset();
res.redirect('/login');
} else{
req.user = user;
req.session.user = user;
// expose the user to the template
res.locals.user = user;
}
next();
});
} else {
next();
}
});
/*
* Middleware function to check if the user is logged in
*/
function isLogedIn(req, res, next){
if(req.user){
next();
} else{
res.redirect('/login');
}
};
/**
* Index Route
*/
app.get('/', function(req, res){
res.render('index');
});
/**
* Login Route
*/
app.get('/login', function(req, res){
res.render('login');
});
/**
* Register Route
*/
app.get('/register', function(req, res){
res.render('register');
});
/**
* Dashboard route
*/
app.get('/dashboard', isLogedIn, function(req, res) {
res.render('dashboard');
});
/**
* Login route when user submit form
*/
app.post('/login', function(req, res){
User.findOne({ 'email': req.body.email }, function(err, user) {
if(err) {
res.render('login');
}
if(!user) {
res.render('login');
} else{
if(bcrypt.compareSync(req.body.password, user.password)) {
//req.session.email = req.body.email;
req.session.user = user;
res.redirect('/dashboard');
} else {
res.render('login');
}
}
});
});
/**
* Register route when user submit form
*/
app.post('/register', function(req, res){
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(req.body.password, salt);
var user = new User({
username : req.body.usernmae,
email : req.body.email,
firstname : req.body.firstname,
lastname : req.body.lastname,
password : hash
});
user.save(function(err){
if(!err) {
req.session.user = user;
res.redirect('/dashboard');
}
});
});
/**
* Logout route
*/
app.get('/logout', function(req, res){
req.session.reset()
res.redirect('/login');
});
app.listen(3000, function(req, res){
console.log('yay! server is up and running');
});