2020-10-29 20:50:36 -07:00
|
|
|
global._CC = { require }
|
2018-11-22 15:42:19 -08:00
|
|
|
const expressSessionLevel = require('express-session-level');
|
2018-11-20 11:19:58 -08:00
|
|
|
const LocalStrategy = require('passport-local').Strategy;
|
2018-11-22 15:42:19 -08:00
|
|
|
const session = require('express-session');
|
2018-11-20 11:19:58 -08:00
|
|
|
const bcrypt = require('bcrypt-nodejs');
|
|
|
|
const flash = require('connect-flash');
|
|
|
|
const passport = require('passport');
|
|
|
|
const express = require('express');
|
2018-11-22 15:42:19 -08:00
|
|
|
const level = require('level');
|
2018-11-20 11:19:58 -08:00
|
|
|
|
|
|
|
const config = require('./config');
|
2020-10-29 11:14:38 -07:00
|
|
|
_CC.config = config
|
2018-11-20 11:19:58 -08:00
|
|
|
|
2020-11-03 17:16:23 -08:00
|
|
|
if (!config.dbPrefix.startsWith('http')) {
|
|
|
|
const mkdirp = require('mkdirp').sync
|
|
|
|
mkdirp(config.dbPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
const PouchDB = require('pouchdb').defaults({ prefix: config.dbPrefix });
|
|
|
|
|
2018-11-20 11:19:58 -08:00
|
|
|
const logger = require('./logger');
|
2020-11-03 17:16:23 -08:00
|
|
|
const { dbExposePort } = require('./config');
|
2018-11-20 11:19:58 -08:00
|
|
|
|
|
|
|
const app = express();
|
2020-10-29 11:14:38 -07:00
|
|
|
app.set('base', config.base)
|
2020-11-02 14:22:07 -08:00
|
|
|
app.set('trust proxy', config.trustProxy)
|
2018-11-20 11:19:58 -08:00
|
|
|
|
2020-11-03 17:16:23 -08:00
|
|
|
const db = new PouchDB('users');
|
2018-11-20 11:19:58 -08:00
|
|
|
|
|
|
|
passport.use('local', new LocalStrategy(
|
|
|
|
(username, password, done) => {
|
2018-12-03 14:03:43 -08:00
|
|
|
username = username.trim();
|
2018-11-20 11:19:58 -08:00
|
|
|
db.get(username)
|
|
|
|
.then(doc => {
|
|
|
|
bcrypt.compare(password, doc.password, (err, correct) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
if (!correct) return done(null, false, { message: 'Incorrect password' });
|
|
|
|
if (correct) return done(null, doc);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if (err.message === 'missing') return done(null, false, { message: 'Incorrect username.' });
|
|
|
|
return done(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
passport.serializeUser((user, callback) => callback(null, user._id));
|
|
|
|
|
|
|
|
passport.deserializeUser((user, callback) => {
|
|
|
|
db.get(user)
|
|
|
|
.then(dbUser => callback(null, dbUser))
|
2020-10-29 20:50:36 -07:00
|
|
|
.catch(() => callback(null, null));
|
2018-11-20 11:19:58 -08:00
|
|
|
});
|
|
|
|
|
2018-11-22 15:42:19 -08:00
|
|
|
|
|
|
|
const LevelStore = expressSessionLevel(session);
|
2019-08-19 07:49:21 -07:00
|
|
|
const sessionDb = level(config.sessionStore)
|
2018-11-22 15:42:19 -08:00
|
|
|
|
2018-11-20 11:19:58 -08:00
|
|
|
app.use(require('body-parser').urlencoded({ extended: true }));
|
2018-11-22 15:42:19 -08:00
|
|
|
app.use(session({
|
|
|
|
secret: config.secret,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true,
|
2018-11-22 16:07:42 -08:00
|
|
|
store: new LevelStore(sessionDb),
|
|
|
|
cookie: {
|
|
|
|
maxAge: config.sessionMaxAge
|
2020-10-29 11:14:38 -07:00
|
|
|
},
|
|
|
|
name: 'christmas_community.connect.sid'
|
2018-11-22 15:42:19 -08:00
|
|
|
}));
|
2018-11-20 11:19:58 -08:00
|
|
|
app.use(flash());
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
|
|
|
|
app.use(require('./middlewares/locals'));
|
|
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
logger.log('express', `${req.ip} - ${req.method} ${req.originalUrl}`);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.set('view engine', 'pug');
|
2020-10-29 11:14:38 -07:00
|
|
|
app.use(config.base, require('./routes')({ db, config }));
|
2018-11-20 11:19:58 -08:00
|
|
|
|
|
|
|
app.listen(config.port, () => logger.success('express', `Express server started on port ${config.port}!`))
|
2020-11-03 17:16:23 -08:00
|
|
|
|
|
|
|
;(() => {
|
|
|
|
if (!dbExposePort) return
|
|
|
|
const dbExposeApp = express()
|
|
|
|
dbExposeApp.use('/', require('express-pouchdb')(PouchDB, { inMemoryConfig: true }));
|
|
|
|
dbExposeApp.listen(config.dbExposePort, () => logger.success('db expose', `DB has been exposed on port ${config.dbExposePort}`))
|
|
|
|
})()
|