-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
177 lines (148 loc) · 4.68 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
express = require('express')
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var session = require('express-session');
var flash = require('connect-flash');
// importált modulok
var Waterline = require('waterline');
var waterlineConfig = require('./config/waterline');
var indexController = require('./controllers/index');
var errorController = require('./controllers/error');
var loginController = require('./controllers/login');
var aboutController = require("./controllers/about");
//-----------------------------------------------------
var hbs = require('hbs');
var blocks = {};
hbs.registerHelper('extend', function(name, context) {
var block = blocks[name];
if (!block) {
block = blocks[name] = [];
}
block.push(context.fn(this));
});
hbs.registerHelper('block', function(name) {
var val = (blocks[name] || []).join('\n');
// clear the block
blocks[name] = [];
return val;
});
//-------------------------------------------
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Local Strategy for sign-up
passport.use('local-signup', new LocalStrategy({
usernameField: 'neptun',
passwordField: 'password',
passReqToCallback: true,
},
function(req, neptun, password, done) {
req.app.models.user.findOne({ neptun: neptun }, function(err, user) {
if (err) { return done(err); }
if (user) {
return done(null, false, { message: 'Létező neptun.' });
}
req.app.models.user.create(req.body)
.then(function (user) {
return done(null, user);
})
.catch(function (err) {
return done(null, false, { message: err.details });
})
});
}
));
// Stratégia
passport.use('local', new LocalStrategy({
usernameField: 'neptun',
passwordField: 'password',
passReqToCallback: true,
},
function(req, neptun, password, done) {
req.app.models.user.findOne({ neptun: neptun }, function(err, user) {
if (err) { return done(err); }
if (!user || !user.validPassword(password)) {
return done(null, false, { message: 'Helytelen adatok.' });
}
return done(null, user);
});
}
));
// Middleware segédfüggvény
function setLocalsForLayout() {
return function (req, res, next) {
res.locals.loggedIn = req.isAuthenticated();
res.locals.user = req.user;
next();
}
}
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login');
}
function andRestrictTo(role) {
return function(req, res, next) {
if (req.user.role == role) {
next();
} else {
next(new Error('Unauthorized'));
}
}
}
//-------------------------------------------
// express app
var app = express();
//config
app.set('views', './views');
app.set('view engine', 'hbs');
//Middleware
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(session({
cookie: { maxAge: 60000 },
secret: 'titkos szoveg',
resave: false,
saveUninitialized: false,
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(setLocalsForLayout());
//endpoints
app.use('/', indexController);
//app.use('/errors', errorController);
app.use('/errors', ensureAuthenticated, errorController);
app.use('/login', loginController);
app.use('/about', aboutController);
app.get('/operator', ensureAuthenticated, andRestrictTo('operator'), function(req, res) {
res.end('operator');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
// ORM példány
var orm = new Waterline();
orm.loadCollection(Waterline.Collection.extend(require('./models/error')));
orm.loadCollection(Waterline.Collection.extend(require('./models/user')));
// ORM indítása
orm.initialize(waterlineConfig, function(err, models) {
//if(err) throw err;
app.models = models.collections;
app.connections = models.connections;
// Start Server
//var port = process.env.PORT || 3000;
//app.listen(port, function () {
// console.log('Server is started.');
//});
var port = process.env.PORT;
var host = process.env.IP;
var server = app.listen(port, host, function(){console.log('Server is started.');});
console.log("ORM is started.");
});