-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
executable file
·60 lines (52 loc) · 1.48 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module.exports = function(middleware) {
var Middleware = {},
mipush = require('./library');
Middleware.hasConfig = function(req, res, next) {
if (mipush.config) next();
else res.status(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('mipush/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();
}
mipush.isUserAssociated(req.user.uid, function(err, assoc) {
if (err) {
return next(err);
}
res.locals.setupRequired = !assoc;
next();
});
};
Middleware.addDevice = function(req, res, next) {
if(!req.body || !req.body.phone || !req.body.regid || !req.user){
return res.status(400).json('Invalid request:'+" body: "+ JSON.stringify(req.body) + " phone:" + req.body.phone + " regid:" + req.body.regid + " user:" + JSON.stringify(req.user));
}
mipush.saveDevice(req.user.uid, req.body.phone, req.body.regid, function(err){
if(err){
return res.status(400).json(err);
}
next();
});
};
return Middleware;
};