-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware.js
81 lines (71 loc) · 2.02 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var path = require("path");
module.exports = function(middleware) {
var Middleware = {},
onesignal = require("./library");
Middleware.hasConfig = function(req, res, next) {
if (onesignal.config && onesignal.config.id && onesignal.config.secret)
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("onesignal/settings");
} else {
middleware.buildHeader(req, res, function() {
res.render("500", {
message: req.query.error
});
});
}
};
Middleware.OneSignalSDKWorker = function(req, res, next) {
res.sendFile("OneSignalSDKWorker.js", {
root: path.join(
__dirname,
"../../build/public/plugins/nodebb-plugin-onesignal/static/lib/"
)
});
};
Middleware.OneSignalSDKUpdaterWorker = function(req, res, next) {
res.sendFile("OneSignalSDKUpdaterWorker.js", {
root: path.join(
__dirname,
"../../build/public/plugins/nodebb-plugin-onesignal/static/lib/"
)
});
};
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();
}
onesignal.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.player_id || !req.user) {
return res.status(400).json("Invalid request");
}
onesignal.savePlayerId(req.user.uid, req.body.player_id, function(err) {
if (err) {
return res.status(400).json(err);
}
next();
});
};
return Middleware;
};