-
Notifications
You must be signed in to change notification settings - Fork 197
/
app.js
97 lines (83 loc) · 2.37 KB
/
app.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
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var routes = require('./server/index');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var config = require('config');
var app = exports.app = express();
var configure, start;
var model = require('./models/');
var loader = function (app, key) {
var loadPath;
loadPath = __dirname + ("/modules/");
app[key] = require(loadPath + key)(app);
return app;
};
passport.use(new LocalStrategy(
{
usernameField: 'name',
passwordField: 'password'
},
function (username, password, done) {
model.User.find({where: {name: username}}).then(function (user) {
if (!user) {
done(null, false, {message: 'Unknown user'});
} else {
user.comparePassword(password, function (err, result) {
if (result) {
done(null ,true);
} else {
done(null, false, {message: 'Password || Username error'})
}
});
}
}).error(function (err) {
done(err);
});
}
));
passport.serializeUser(function (user, done) {
done(null, user.uid);
});
passport.deserializeUser(function (uid, done) {
model.User.find({where: {uid: uid}})
.then(function (user) {
done(null, user);
}).error(function (err) {
console.log(id, err);
done(err, null);
});
});
configure = function () {
app.set('views', path.join(__dirname + '/server', 'views'));
app.set('view engine', 'jade');
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(session({
secret: config.get('secret'),
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
if(config.get('logging')){
app.use(logger('dev'));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname + '/server', 'public')));
app.use('/', routes);
app.config = config;
var modules = config.get('modules');
for (var i = 0; i < modules.length; i++) {
app = loader(app, modules[i]);
}
return app;
};
module.exports.app = app;
module.exports.configure = configure;