-
Notifications
You must be signed in to change notification settings - Fork 13
/
middleware.js
44 lines (39 loc) · 1.07 KB
/
middleware.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
module.exports = function(middleware) {
var Middleware = {},
Pushbullet = require('./library');
Middleware.hasConfig = function(req, res, next) {
if (Pushbullet.config && Pushbullet.config.id && Pushbullet.config.secret) next();
else res.redirect('404');
};
Middleware.hasCode = function(req, res, next) {
if (req.query && req.query.code) {
next();
} else if (req.query.hasOwnProperty('error') && req.query.error === 'access_denied') {
res.redirect('pushbullet/settings');
} else {
middleware.buildHeader(req, res, function() {
res.render('500', {
message: req.query.error
});
});
}
};
Middleware.isLoggedIn = function(req, res, next) {
if (req.user && parseInt(req.user.uid, 10) > 0) next();
else res.redirect('403');
};
Middleware.setupRequired = function(req, res, next) {
if (!req.user) {
res.locals.setupRequired = false;
return next();
}
Pushbullet.isUserAssociated(req.user.uid, function(err, assoc) {
if (err) {
return next(err);
}
res.locals.setupRequired = !assoc;
next();
});
};
return Middleware;
};