forked from mkellsy/homebridge-ui
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
executable file
·97 lines (76 loc) · 2.24 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 favicon = require("serve-favicon");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var passport = require("passport");
var strategy = require("passport-local").Strategy;
var users = require('./users');
app = express();
app.use(express.static(path.join(__dirname, "public")));
app.engine("html", require("hogan-express"));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "html");
app.set("layout", "layout");
app.set("auths", require(hb.auth));
app.use(cookieParser());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(require("express-session")({
secret: "keyboard cat",
resave: false,
saveUninitialized: false
}));
passport.use(new strategy(function (username, password, callback) {
users.findByUsername(username, function (err, user) {
if (err) {
return callback(err);
}
if (!user) {
return callback(null, false);
}
if (user.password != password) {
return callback(null, false);
}
return callback(null, user);
});
}));
passport.serializeUser(function (user, callback) {
callback(null, user.id);
});
passport.deserializeUser(function (id, callback) {
users.findById(id, function (err, user) {
if (err) {
return callback(err);
}
callback(null, user);
});
});
app.use(passport.initialize());
app.use(passport.session());
var index = require("./routes/index");
var log = require("./routes/log");
var accounts = require("./routes/accounts");
var plugins = require("./routes/plugins");
var config = require("./routes/config");
app.use("/", index);
app.use("/log", log);
app.use("/accounts", accounts);
app.use("/plugins", plugins);
app.use("/config", config);
app.use(function (req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
app.use(function (err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
res.status(err.status || 500);
res.render("error", {
controller: "error",
title: "Error"
});
});
module.exports = app;