fix build error, deploy using docker-compose works, misc changes using global MQTT instead of local one to tap to already existin broker
115 lines
3.4 KiB
JavaScript
115 lines
3.4 KiB
JavaScript
var express = require('express');
|
|
var passport = require('passport');
|
|
var Strategy = require('passport-local').Strategy;
|
|
var db = require('./db');
|
|
|
|
|
|
// Configure the local strategy for use by Passport.
|
|
//
|
|
// The local strategy require a `verify` function which receives the credentials
|
|
// (`username` and `password`) submitted by the user. The function must verify
|
|
// that the password is correct and then invoke `cb` with a user object, which
|
|
// will be set at `req.user` in route handlers after authentication.
|
|
passport.use(new Strategy({
|
|
passReqToCallback: true},
|
|
function(username, password, cb) {
|
|
console.log('requesting authentication for user '+ username);
|
|
db.users.findByUsername(username, function(err, user) {
|
|
if (err) { return cb(err); }
|
|
if (!user) { return cb(null, false); }
|
|
if (user.password != password) { return cb(null, false); }
|
|
return cb(null, user);
|
|
});
|
|
}));
|
|
|
|
|
|
// Configure Passport authenticated session persistence.
|
|
//
|
|
// In order to restore authentication state across HTTP requests, Passport needs
|
|
// to serialize users into and deserialize users out of the session. The
|
|
// typical implementation of this is as simple as supplying the user ID when
|
|
// serializing, and querying the user record by ID from the database when
|
|
// deserializing.
|
|
passport.serializeUser(function(user, cb) {
|
|
cb(null, user.id);
|
|
});
|
|
|
|
passport.deserializeUser(function(id, cb) {
|
|
db.users.findById(id, function (err, user) {
|
|
if (err) { return cb(err); }
|
|
cb(null, user);
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
// Create a new Express application.
|
|
var app = express();
|
|
|
|
// Configure view engine to render EJS templates.
|
|
//app.set('views', __dirname + '/views');
|
|
//app.set('view engine', 'ejs');
|
|
app.set('view engine', 'vash');
|
|
app.use(express.static('public'))
|
|
|
|
// Use application-level middleware for common functionality, including
|
|
// logging, parsing, and session handling.
|
|
app.use(require('morgan')('combined'));
|
|
app.use(require('body-parser').urlencoded({ extended: true }));
|
|
app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
|
|
|
|
// Initialize Passport and restore authentication state, if any, from the
|
|
// session.
|
|
app.use(passport.initialize());
|
|
app.use(passport.session());
|
|
|
|
// Define routes.
|
|
app.get('/',
|
|
function(req, res) {
|
|
res.render('layout', { user: req.user });
|
|
});
|
|
|
|
app.get('/login',
|
|
function(req, res){
|
|
res.render('login');
|
|
});
|
|
|
|
app.post('/login',
|
|
passport.authenticate('local', { failureRedirect: '/login' }),
|
|
function(req, res) {
|
|
res.redirect('/');
|
|
});
|
|
|
|
app.get('/logout',
|
|
function(req, res){
|
|
req.logout();
|
|
res.redirect('/');
|
|
});
|
|
|
|
app.get('/accontrol',
|
|
// passport.authenticate('local', {
|
|
// failureRedirect: '/login' ,
|
|
// successRedirect: '/accontrol'}),
|
|
require('connect-ensure-login').ensureLoggedIn('/login'),
|
|
function(req, res){
|
|
res.render('accontrol', { user: req.user });
|
|
});
|
|
|
|
//app.listen(81);
|
|
|
|
try{
|
|
var fs = require("fs");
|
|
var https = require('https');
|
|
var privateKey = fs.readFileSync('/etc/letsencrypt/live/iot.d-popov.com/privkey.pem', 'utf8');
|
|
var certificate = fs.readFileSync('/etc/letsencrypt/live/iot.d-popov.com/cert.pem', 'utf8');
|
|
var credentials = {key: privateKey, cert: certificate};
|
|
|
|
var httpsServer = https.createServer(credentials, app);
|
|
httpsServer.listen(8443, () => {
|
|
console.log('HTTP server listening on port 8443');
|
|
});
|
|
}catch(e){
|
|
console.log("failed to start HTTPS: " + e.toString())
|
|
}
|