-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
136 lines (125 loc) · 4.86 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
/**
* Server: the program's entry point
*/
var express = require('express')
//routes
, home = require('./routes')
, admin = require('./routes/admin')
, user = require('./routes/user')
, bkmrk = require('./routes/bkmrk')
//stuff
, http = require('http')
, path = require('path')
//database
// , mongoDB = require('./lib/mongodb')
, mongoose = require('mongoose')
//data providers
//, dataProvider = require('./apps/models/dataprovider')
// , userDataProvider = require('./apps/models/userdataprovider')
//models
, bkmrkModel = require('./apps/models/bookmarkmodel')
// , userModel = require('./apps/models/usermodel')
// , adminModel = require('./apps/models/adminmodel')
// , tagModel = require('./apps/models/tagmodel')
, env = require('tq-topicmap');
var TopicMapEnvironment = env;
//NOTE: we can add username/password to this
//@see http://docs.mongodb.org/manual/reference/connection-string/
//Two collections (for now): proxies and users
//var proxyCollection = mongoDB.collection('proxies');
//var userCollection = mongoDB.collection('users');
var DataProvider = TopicMapEnvironment.getDataProvider();
console.log("ABC "+DataProvider);
var connect = require('connect');
//start mongoose might want to put a password here
mongoose.connect('mongodb://127.0.0.1:27017/userdb');
var passport = require('passport')
, User = require('./apps/models/account')
, LocalStrategy = require('passport-local').Strategy;
// , BasicStrategy = require('passport-http').BasicStrategy;
passport.use(new LocalStrategy(function(username,password,done) {
console.log(username+"//"+password+" is trying to login as local.");
//var User = mongoose.model('User');
User.findOne({'username':username}, function(err,puser) {
console.log(err+' | '+puser);
if(!puser){
console.log("user not found.");
return done(null, false, { message: 'Unknown user ' + username });
}
console.log(puser);
console.log(puser.username+' '+puser.password);
puser.comparePassword(password, function(err, isMatch) {
console.log('A '+err);
if (err) return done(err);
if(isMatch) {
return done(null, User);
} else {
return done(null, false, { message: 'Invalid password' });
}
});
//if (password!==puser.password) {
// console.log("password invalid. "+puser.password);
// return done(null, false, { message: 'Invalid password' });
//}
return done(null, puser);
});
}));
passport.serializeUser(function(user, done) {
console.log('serializeUser '+user);
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
console.log('deserializeUsesr '+id);
User.findById(id,function(err,user){
if(err) done(err);
if(user){
done(null,user);
}else{
User.findById(id, function(err,user){
if(err) done(err);
done(null,user);
});
}
});
});
/////////////
// The Express App
/////////////
var app = express()
, bodyParser = require('body-parser')
, cookieParser = require('cookie-parser')
, flash = require('connect-flash')
, logger = require('logger').createLogger('development.log');
var exphbs = require('express3-handlebars');
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//app.use(logger); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth) https://github.com/expressjs/cookie-parser
app.use(bodyParser()); // get information from html forms https://github.com/expressjs/body-parser
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.use(express.static(path.join(__dirname, 'public')));
//required for passport
//app.use(express.session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
//var routes = require('./routes/routes');
require('./routes/routes.js')(app, passport);
/*app.get('/', home.index); //look for index.handlebars
app.get('/admin', admin.admin); //look for admin.handlebars
app.get('/bkmrk', bkmrk.bkmrk); //look for bkmrk.handlebars
//TODO we can add plugin views here
app.get('/users', user.list);
*/
//initialize the models
//dataProvider.init(mongoDB,proxyCollection);
bkmrkModel.init(DataProvider);
//userDataProvider.init(mongoDB,userCollection);
//userModel.init(userDataProvider);
//adminModel.init(userDataProvider);
//tagModel.init(dataProvider);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});