-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·95 lines (83 loc) · 3.06 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
// KeystampDemo
// =============================================================================
// Author : Jean-Philippe beaudet @s3r3nity
//
// app.js
//
// Keystamp-demo server
// =============================================================================
var express = require('express');
var http = require('http');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var logger = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var request = require('request')
//main config
var app = express();
config = require('./config.js');
var BASE_URL = config.api
var server = require('http').createServer(app);
app.set('port', process.env.PORT || config.node_web_server_port);
app.set('views', path.join(__dirname + '/views'));
app.set('view engine', 'jade');
app.set('view options', { layout: true });
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(require('express-session')({
secret: '88677fGG%6%$4',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'app')));
// routes
app.use(function(req, res, next) {
if(req.user) {
if( !req.session.token || !req.session.uid || !req.session.token){
Account.findOne({"username": req.user.username}, function(err, usr) {
if (err || !usr){
console.log('user could not be found')
}
req.session.uid = usr.uid
req.session.xpub = usr.user_pub_key || 'xpub661MyMwAqRbcEyEs9gV77y1QamusXKgsbahZfALFuzyeYj6aFE1Pu6osg9VDdL3ysYXUf8RrQVhzFotuFDe4ZU9coQhUak88ore5T7JSGmF'
req.session.save()
console.log('session uid:'+req.session.uid )
request.post({url: BASE_URL+'/auth',form: {app_secret:config.app_secret, app_id: config.app_id}},function (error, response, body) {
console.log('app id '+config.app_id)
config.token = JSON.parse(body).token
req.session.token = JSON.parse(body).token
req.session.save()
console.log('session token:'+req.session.token )
next()
});
})
}else{
next()
}
}else{
next()
}
})
require('./routes/routes')(app);
console.log(("Express server listening on port " + app.get('port')));
//passport config
var Account = require('./models/account');
passport.use(new LocalStrategy(Account.authenticate()));
passport.serializeUser(Account.serializeUser());
passport.deserializeUser(Account.deserializeUser());
// mongoose
mongoose.connect('mongodb://'+config.node_web_server_host+ '/'+config.mongodb_database);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log("Keystamp-demo db started on : "+ config.mongodb_database+ ": "+config.node_web_server_host);
});
server.listen(config.node_web_server_port);